How to change a cell array into vector in matlab?
20,138
Solution 1
>> myCell = {[1 2],[3 4],[5 6]}; %// example cell. Can have any size/shape
>> result = cell2mat(myCell(:)) %// linearize and then convert to matrix
result =
1 2
3 4
5 6
To plot:
plot(result(:,1),result(:,2),'o') %// or change line spec
Solution 2
Another way to accomplish this:
c = {[1 2], [3 4], [5 6]};
v = vertcat(c{:}); % same as: cat(1,c{:})
plot(v(:,1), v(:,2), 'o')
Cell arrays in MATLAB can be expanded into a comma-separated list, so the above call is equivalent to: vertcat(c{1}, c{2}, c{3})
Author by
Biju
Updated on January 25, 2020Comments
-
Biju over 4 years
I have a cell array which each cell is a point on (x,y) coordination (i.e, the cells are of size [1x2]). Is it possible to change it to matrix that those coordination points to be reserved?
Because when I used cell2mat, the peculiar coordination was change to the size of [1x1] while I need the coordinates.
my cell array is like this:
[0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]
How I can change it to a vector that these coordinates can be used later for plotting?
-
Biju over 10 yearsthanks Luis, but I want to plot these coordinates of [x y], while they are in cell format and don't need to get them seperated
-
Luis Mendo over 10 yearsIn your question you say "change it to matrix"... Anyway, see updated answer
-
Muhammad Usman Saleem over 6 years@LuisMendo what about
myCell = {[1 2 3],[3 4 ],[5 6 5 6 7]}
then how can i convert this cell array to vector? -
Muhammad Usman Saleem over 6 yearswhat about
c= {[1 2 3],[3 4 ],[5 6 5 6 7]}
then how can i convert this cell array to vector? -
Amro over 6 years
-
Amro over 6 yearsotherwise you can pad each cell with NaNs so that all rows have the same length before concatenation. You can find plenty of similar questions here on Stack Overflow, for example this one: stackoverflow.com/q/3054437/97160