如何在matlab中将单元阵列转换成矢量?

时间:2021-10-21 15:57:54

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?

我有一个单元格数组,每个单元格都是一个点(x,y)的协调(I。e,细胞大小为[1x2]。是否有可能将这些协调点保留到矩阵中?

Because when I used cell2mat, the peculiar coordination was change to the size of [1x1] while I need the coordinates.

因为当我使用cell2mat时,当我需要坐标时,特殊的协调会改变为[1x1]的大小。

my cell array is like this: [0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]

我的单元阵列是这样的:[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?

我怎么能把它变成一个向量这些坐标可以用来做绘图?

3 个解决方案

#1


5  

>> 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

#2


3  

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})

在MATLAB中,单元阵列可以扩展为一个逗号分隔的列表,因此上述调用等价于:vertcat(c {1}, c {2}, c {3})

#3


0  

myCell = {[0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]};
hold on;
cellfun(@(c) plot(c(1),c(2),'o'),myCell);

#1


5  

>> 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

#2


3  

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})

在MATLAB中,单元阵列可以扩展为一个逗号分隔的列表,因此上述调用等价于:vertcat(c {1}, c {2}, c {3})

#3


0  

myCell = {[0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]};
hold on;
cellfun(@(c) plot(c(1),c(2),'o'),myCell);