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 vues (au cours des 30 derniers jours)
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 commentaires
Sriram Tadavarty
Sriram Tadavarty le 13 Mai 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.

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 13 Mai 2020
Modifié(e) : Rik le 13 Mai 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);
  2 commentaires
nehohmee
nehohmee le 13 Mai 2020
Oh my so that's why it didn't work! Thank you so much for spotting that mistake of mine. Have a great day ahead!
Rik
Rik le 13 Mai 2020
Happy to help. If you feel my answer solved your issue, please consider marking it as accepted answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by