Arrays 如何在Matlab中绘制单元阵列
我在Matlab中建模SIR疾病传播模型,我有一个网格,它是一个细胞阵列,每个细胞表示一个状态(s,I,r) 我想用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
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