How to extract every elements with different dimension at multiple cells in efficient manner?
1 view (last 30 days)
Show older comments
Dear all,
Say I have three cells, where the element
dd = [49;50;51;52;53;54] [55;56;57] [85;86;87;88;89;90]
The objective is to extract all the element in each cell and make it into single row, such that
[40 50 51 52 53 54 55 56 57 85 86 87 88 89 90]
I used the following lines.
C =1
for i =1:size(dd,2)
Xcx = cell2mat (dd (1,i));
ee =[]
ee (1,1: length (Xcx)) =Xcx;
for k =1: length (ee )
new (C,1) = ee (k)
C =C+1
end
end
Even though the code do the trick, I am looking if there is more advance and compact ways of doing it?
Thanks in advance
0 Comments
Accepted Answer
More Answers (2)
Sanjay Nair
on 2 Aug 2017
Hello,
If I'm understanding your problem correctly you have a cell array that can be defined in the following manner
dd = {[49;50;51;52;53;54], [55;56;57], [85;86;87;88;89;90]};
Since you have a cell array of column vectors with only three elements, perhaps the easiest way to extract them into a single row vector would be to transpose and concatenate the contents in a single command as follows:
row = [dd{1}', dd{2}', dd{3}'];
You can get more information about accessing data in cell-arrays in: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html
Akira Agata
on 3 Aug 2017
How about using cellfun and horzcat functions as follows. In this code, each element in cell array dd is transposed by cellfun. Next, horzcat concatenate each element horizontally.
dd = {[49;50;51;52;53;54], [55;56;57], [85;86;87;88;89;90]};
ddTrans = cellfun(@transpose,dd,'UniformOutput',false);
Output = horzcat(ddTrans{:});
See Also
Categories
Find more on Creating and Concatenating Matrices 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!