How can I convert my matrix of 61 x 1 cells (60 cells of 10 x 1 and 1 cell of 3 x 1) to an array of 603 x 1?

3 views (last 30 days)
I have a matrix consisting of 61 x 1 cells, with 60 cells of 10 x 1 and 1 cell of 3 x 1. How can I convert it into an array of 603 x 1?
cell2mat does not work as I have 1 cell that is sized differently from the other 60 cells. However, I would need to maintain the size of 603 x 1 as I will need it to match the size of another matrix for correlation purposes.
I really appreciate your help!
  6 Comments
Sriram Tadavarty
Sriram Tadavarty on 13 May 2020
Yeah. It will just work without any issue. You can try modifying the value of 61 above with some other value, as shown below:
a = cell(61,1);
a(1:61) = {rand(10,1)};
index = randi([1 61],1,1); % Get random index to place 3 x 1 elements
a(index) = {rand(3,1)}; % This 3 x 1 is placed randomly in the 61 cell array
aM = cell2mat(a);
Hope this helps.

Sign in to comment.

Answers (1)

Rik
Rik on 13 May 2020
Edited: Rik on 13 May 2020
If you look closer you will see that cell 61 is not 3x1, but 1x3.
%generate sample data
data=cell(61,1);
for n=1:61
data{n}=n+rand(10,1);
end
data{randi(end)}=rand(1,3);%yes, 1x3 not 3x1, see your screenshot
%linearize every cell that has more than 1 column
for n=( find(cellfun('size',data,2)>1) )'
data{n}=data{n}(:);
end
%convert to 603x1 vector
a=cell2mat(data);

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!