2016-11-15 73 views
0

我在Matlab中对SIR疾病扩展模型进行建模,我有一个网格,它是一个单元阵列,每个单元意味着一个状态(状态为(s,i,r))。如何在Matlab中绘制单元阵列

我想用s作为蓝色圆点和i作为红色圆点绘制网格,轴线为length(Grid)

Grid = 

    []  [] 's' 's'  []  []  [] 'i'  []  [] 
    's'  [] 's' 's'  []  []  []  [] 's'  [] 
    []  []  []  []  []  []  []  [] 's'  [] 
    []  []  [] 's'  []  []  []  []  []  [] 
    [] 's'  [] 'i' 's'  [] 's' 'i'  [] 's' 
    []  [] 's' 's'  [] 's'  [] 'i' 's' 'i' 
    'i'  []  []  [] 's'  []  []  []  []  [] 
    []  []  [] 's'  []  []  []  []  []  [] 
    [] 's'  []  []  []  [] 'i' 'i' 'i'  [] 
    []  [] 's'  [] 's' 's'  []  []  []  [] 

回答

2

您可以使用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', []}

enter image description here

如果你想实际的点,而不是测试,你可以做这样的事情:

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