Is cell2mat supposed to support non-plaid cell partitioning? If not, why not?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Matt J
il 30 Mar 2022
Commentato: Bjorn Gustavsson
il 31 Mar 2022
I have 2 cell arrays,
load C; C,Ct
Why does cell2mat work on one,
cell2mat(C)
but not the other?
cell2mat(Ct)
I understand that the cell partitioning of Ct is not plaid, but nor is it for C.
3 Commenti
Rik
il 30 Mar 2022
I would not have expected any non-plaid partitions to work, but that first example is straight from the documentation and works as far back as ML6.5 (from 20 years ago).
The documention does state this: "For example, the contents of cells in the same column must have the same number of columns, although they need not have the same number of rows". However, it is unclear why this limitation does not apply to the rows of the cell array.
Either the documentation is not clear enough that the rows are less restricted than the columns, or this is not intended.
Since the documentation doesn't suggest this limitation, I would personally consider this a bug.
Risposta accettata
Bruno Luong
il 30 Mar 2022
But this one works
Ctlarge={rand(1,2) rand(1,1) rand(1,0); rand(3,1) rand(3,2) rand(3,0)}
cell2mat(Ctlarge)
You can edit the function to see why !
2 Commenti
Bruno Luong
il 30 Mar 2022
Modificato: Bruno Luong
il 30 Mar 2022
Yes, longest in term of cell length not final array length. In case of draw, working on columns first.
Più risposte (1)
Rik
il 30 Mar 2022
This doesn't answer your actual question, but below is code that should work on all combinations of non-plaid partitions. Some of the code is already ND, while other parts are limited to 1/2D. It isn't perfect, but it works on this example. I didn't spend much time optimizing to limit data copies etc, so the performance will be terrible. You can adapt the first loop to check if you need this function in the first place.
C={[5;9],[2 3 4];
1,[6 7 8;10 11 12]};
%create fully transposed array
Ct=C.';for n=1:numel(Ct),Ct{n}=Ct{n}.';end
cell2mat_slow_nonplaid(C)
cell2mat_slow_nonplaid(Ct).' % <-- transpose yet again for a sanity check
function out=cell2mat_slow_nonplaid(C)
% replicate the function of cell2mat for non-plaid partitions
%check if the number of elements along a dimension is the same over the cell array (i.e. is the
%total number of columns the same for each column in the cell array)
sz=cell(1,max(cellfun(@ndims,C(:))));
for dim=1:numel(sz)
sz{dim}=cellfun('size',C,dim);
sz{dim}=unique(sum(sz{dim},dim));
if 1 ~= numel(sz{dim})
error('dimension %d doesn''t match',dim)
end
end
sz=horzcat(sz{:});
%determine the number of rows preceeding each element
rows_preceeding=zeros(size(C));
dim=1;
for r=2:size(C,1) % skip the first row
for c=1:size(C,2)
rows_preceeding(r,c)=size(C{r-1,c},dim);
end
end
rows_preceeding=cumsum(rows_preceeding,dim);
%determine the number of columns preceeding each element
cols_preceeding=zeros(size(C));
dim=2;
for r=1:size(C,1)
for c=2:size(C,2) % skip the first col
cols_preceeding(r,c)=size(C{r,c-1},dim);
end
end
cols_preceeding=cumsum(cols_preceeding,dim);
%store the parts in the final array
out=cell(sz);
for n=1:numel(C)
r=rows_preceeding(n)+(1:size(C{n},1));
c=cols_preceeding(n)+(1:size(C{n},2));
tmp=num2cell(C{n});
out(r,c)=tmp;
end
%all elements should contain exactly 1 value now
%we only need to unwind this without affecting the type
out=cell2mat(out);
end
2 Commenti
Bjorn Gustavsson
il 31 Mar 2022
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!