From bug-request at octave dot org Tue Dec 13 20:03:37 2005 Subject: Re: [OctDev] Proposed changes to cell2mat and inclusion in Octave From: William Poetra Yoga Hadisoeseno To: octave-dev at lists dot sourceforge dot net, bug@octave.org Date: Wed, 14 Dec 2005 10:01:56 +0800 ------=_Part_7844_14664495.1134525716103 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Part of the discussion on octave-dev follows: On 12/9/05, David Bateman wrote: > William Poetra Yoga Hadisoeseno wrote: > > >On 12/9/05, David Bateman wrote: > > > > > >>Again, why? Here is matlab's output > >> > >> >> C =3D {[1], [2, 3, 4; 6 7 8]; [5; 9],[10 11 12]} > >> >> cell2mat(C) > >>??? Error using =3D=3D> cat > >>CAT arguments dimensions are not consistent. > >> > >>Error in =3D=3D> cell2mat at 92 > >> m{n} =3D cat(2,c{n,:}); > >> > >>The error message seems to indicate that matlab just lets cat generate > >>the error as well. > >> > >> > >> > > > >Well, in that case then we should just let cat do the job ;) > > > > > > > >>Matlab doesn't respect their own documentation either > >> > >> >> C =3D {[1, 2], [3, 4]; [5; 9],[6 7 8; 10 11 12]}; > >> >> cell2mat(C) > >> > >>ans =3D > >> > >> 1 2 3 4 > >> 5 6 7 8 > >> 9 10 11 12 > >> > >>This is with R14sp2.. > >> > >> > >> > > > >OK, this is consistent with the old (current) cell2mat. > > > > > > > >> >> C =3D {[1;2],ones(2,2)=3D=3D1} > >> > >>C =3D > >> > >> [2x1 double] [2x2 logical] > >> > >> >> cell2mat(C) > >>??? Error using =3D=3D> cell2mat > >>All contents of the input cell array must be of the same data type. > >> > >>However, this seems stupid to me, as logical arrays are supposed to be > >>promoted to double in normal usage... I'd prefer that the above remaine= d > >>legal.. > >> > >> > >> > > > >Yes, I also think the above should work (logical -> numeric). Also, I > >think char arrays should be allowed (using ascii). What do you think? > > > > > > > Promotion of type is implicit in the concatenation operation. If cat > allows it so should cell2mat in my opinion. Therefore just letting cat > generate the error messages for an illegal concatenation of types makes > sense to me, as it will report the two offending types in the > concatenation operation. I believe this means that cell2mat needs no > changes... > > Regards > David > > > -- > David Bateman David dot Bateman at motorola dot com > 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 > > It seems that no one has objections at this point, so I'm braving myself to submit this file (imported from octave-forge). The file is attached. -- William Poetra Yoga Hadisoeseno ------=_Part_7844_14664495.1134525716103 Content-Type: text/plain; name=cell2mat.m.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cell2mat.m.txt" ## Copyright (C) 2005 Laurent Mazet ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## -*- texinfo -*- ## at deftypefn {Function File} {@var{m} =} cell2mat (@var{c}) ## ## Converts the cell array at var{c} into matrix @var{m} by concatenating all ## elements of at var{c} into a hyperrectangle. Elements of @var{c} must be ## of type numeric, logical or char, and at code{cat} must be able to ## concatenate them together. ## at end deftypefn ## ## at seealso{mat2cell, num2cell} ## 2005-01-13 ## initial release function m = cell2mat (c) if (nargin != 1) usage ("m = cell2mat (c)"); endif if (! iscell (c)) error ("cell2mat: c is not a cell array"); endif nb = numel(c); if (nb == 0) m = []; elseif (nb == 1) if (isnumeric (c{1}) || ischar (c{1}) || islogical (c{1})) m = c{1}; elseif (iscell (c{1})) m = cell2mat (c{1}); else error("cell2mat: all elements in c must be of type numeric, logical or char"); endif else ## n dimensions case for k = (ndims (c)):-1:2, sz = size (c); sz(end) = 1; c1 = cell (sz); for p = 1:(prod (sz)) c1{p} = cat (k, c{p:(prod (sz)):end}); endfor c = c1; endfor m = cat (1, c1{:}); endif endfunction ## Tests %!shared C, D, E, F %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; %! D = C; D(:,:,2) = C; %! E = [1 2 3 4; 5 6 7 8; 9 10 11 12]; %! F = E; F(:,:,2) = E; %!assert (cell2mat (C), E); %!test %! if ([1e6,1e4,1] * str2num (split (version, '.')) > 2010064) %! assert (cell2mat (D), F); % crashes octave 2.1.64 %! endif ## Demos %!demo %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; %! cell2mat (C) ------=_Part_7844_14664495.1134525716103 Content-Type: text/plain; name=cell2mat.changelog.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cell2mat.changelog.txt" 2005-12-14 William Poetra Yoga Hadisoeseno * cell/cell2mat.m: Import from octave-forge. Tidied up coding style. Use p instead of i as counter. ------=_Part_7844_14664495.1134525716103-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------