Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在Matlab中绘制单元阵列_Arrays_Matlab_Plot_Cell - Fatal编程技术网

Arrays 如何在Matlab中绘制单元阵列

Arrays 如何在Matlab中绘制单元阵列,arrays,matlab,plot,cell,Arrays,Matlab,Plot,Cell,我在Matlab中建模SIR疾病传播模型,我有一个网格,它是一个细胞阵列,每个细胞表示一个状态(s,I,r) 我想用s作为蓝点和I作为红点绘制网格,轴采用长度(网格) 您可以使用ismember查找每个标签在单元格数组中的位置。第二个输出将提供标签的索引。然后,您可以使用带有自定义颜色映射的imagesc来显示结果 % Create a copy of Grid where the empty cells are replaced with '' tmp = Grid; tmp = cellfu

我在Matlab中建模SIR疾病传播模型,我有一个网格,它是一个细胞阵列,每个细胞表示一个状态(s,I,r)

我想用
s
作为蓝点和
I
作为红点绘制网格,轴采用
长度(网格)


您可以使用
ismember
查找每个标签在单元格数组中的位置。第二个输出将提供标签的索引。然后,您可以使用带有自定义颜色映射的
imagesc
来显示结果

% Create a copy of Grid where the empty cells are replaced with ''
tmp = Grid;
tmp = cellfun(@(x)['' x], Grid, 'UniformOutput', false);

% Locate all of the 's' and 'i' cells and assign values of 1 and 2 respectively
[~, labels] = ismember(tmp, {'s', 'i'});

% Display the resulting label matrix
imagesc(labels)

% Use a custom colormap where empty cells are black, 's' are blue and 'i' are red
cmap = [0 0 0; 0 0 1; 1 0 0];
colormap(cmap)
如果我们用
Grid={'s',[];'i',[]}

如果你想要实际的点,你可以这样做:

colors = {'r', 'b'};
labels = {'s', 'i'};

for k = 1:numel(labels)
    % Find the row/column indices of the matches
    [r, c] = find(cellfun(@(x)isequal(x, labels{k}), Grid));

    % Plot these at points using the specified color        
    plot(c, r, '.', 'Color', colors{k}, 'MarkerSize', 20);
    hold on
end

您可以使用
ismember
查找每个标签在单元格数组中的位置。第二个输出将提供标签的索引。然后,您可以使用带有自定义颜色映射的
imagesc
来显示结果

% Create a copy of Grid where the empty cells are replaced with ''
tmp = Grid;
tmp = cellfun(@(x)['' x], Grid, 'UniformOutput', false);

% Locate all of the 's' and 'i' cells and assign values of 1 and 2 respectively
[~, labels] = ismember(tmp, {'s', 'i'});

% Display the resulting label matrix
imagesc(labels)

% Use a custom colormap where empty cells are black, 's' are blue and 'i' are red
cmap = [0 0 0; 0 0 1; 1 0 0];
colormap(cmap)
如果我们用
Grid={'s',[];'i',[]}

如果你想要实际的点,你可以这样做:

colors = {'r', 'b'};
labels = {'s', 'i'};

for k = 1:numel(labels)
    % Find the row/column indices of the matches
    [r, c] = find(cellfun(@(x)isequal(x, labels{k}), Grid));

    % Plot these at points using the specified color        
    plot(c, r, '.', 'Color', colors{k}, 'MarkerSize', 20);
    hold on
end