سؤال

In MatLab, all cells in my 60x1-cellarray contain a 10x1 double.

I would like to concatenate all these doubles vertically, except for the first number in every double.

My failed attempts was:

CellArray={[1 2 3];[1 2 3];[1 2 3]}
ContacenatedCellArray = vertcat(CellArray{:,1}(2:end))

This, obviously, did not work becauce CellArray{:,1} refers to multiple cells so that (2:end) is a bit silly.

Do you have any suggestions?

Thanks in advance!

هل كانت مفيدة؟

المحلول

Why not just do it in two lines:

temp = vertcat(CellArray{:}); %// or cell2mat(CellArray)
temp2 = temp(:,2:end)';
ContacenatedCellArray = temp2(:);

نصائح أخرى

Try this -

%%// Vertically concatenated array
ContacenatedCellArray = cell2mat(CellArray); 

%%// Use the first index of every double array to remove those
ContacenatedCellArray(1:10:end)=[]; 

Okay. I found a workaround. Just delete that first double after contacenating everything. Not pretty but it works...

ContacenatedCellArray(1:length(CellArray{1,1}):end)=[];

Thanks for your help!

There is this one-line solution where selection is done before concatenation

cell2mat(arrayfun(@(x) x{1}(2:end), CellArray, 'UniformOutput', 0))

Input & output

CellArray={(1:4)';(1:4)';(1:4)'}

ans =

     2
     3
     4
     2
     3
     4
     2
     3
     4
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top