From: David B. <Dav...@mo...> - 2005-12-08 14:41:59
|
William Poetra Yoga Hadisoeseno wrote: >The blkdiag.m file I imported into Octave is dependent on cell2mat, >but I think it's not quite ready for inclusion into Octave yet. So I >modified the file (attached). Maybe the original author (Laurent >Mazet) and others would like to comment on it before I post it on >bu...@oc...? > >Due to my lack of intelligence, I've so far failed to completely >understand the following code by Laurent (hey, but it works!) (it's >been reformatted): > >for p = (ndims (c)):-1:2, > sz = size (c); > sz(end) = 1; > c1 = cell (sz); > for q = 1:(prod (sz)) > c1{q} = cat (p, c{q:(prod (sz)):end}); > endfor > c = c1; >endfor >m = cat (1, c1{:}); > >Can anyone explain it to me? > >Note: I changed the wording of the license from "this program" to >"Octave". Is it OK? > >-- >William Poetra Yoga Hadisoeseno > > > It just does a cat along the last dimension of the cell array so that for example c = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; becomes c1 = {[1, 2, 3, 4] ; [5 6 7 8; 9 10 11 12]} after the first interation. For 2-D there is only one iteration of the loop and the last cat operation is handled by "m = cat (1, c1{:})". The fact is I'm not sure what you are trying to achieve with your changes. What was wrong with the previous implementation? cell2mat.m itself needs no argument dimension checking, as if the arguments are wrong, the dimension checking in the "cat" operation will cause an error and exit from cell2mat. For example with the current version of cell2mat I get the following, when passing it matrices that can't be concatenated. octave:2> C = {[1], [2 3 4]; [5; 9; 13], [6 7 8; 10 11 12]}; octave:3> cell2mat(C) error: cat: dimension mismatch error: evaluating assignment expression near line 65, column 14 error: evaluating for command near line 64, column 5 error: evaluating for command near line 60, column 3 error: called from `cell2mat' in file `/opt/octave-2.9/share/octave/2.9.4/site/m/octave-forge/cell/cell2mat.m' Note that although C = {[1, 2], [3, 4]; [5; 9],[6 7 8; 10 11 12]}; cell2mat(C) works, the generalization C = {[1], [2, 3, 4; 6 7 8]; [5; 9],[10 11 12]} cell2mat(C) doesn't, even though conceptually the meaning is clear in terms of the hyper-rectangle. In general there must be a grid in the hyper-rectangular along which all matrices fill the elements of this grid. Matlab has the same behaviour. Regards David -- David Bateman Dav...@mo... Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary |