2016-11-15 2 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を使用すると、セル配列内の各ラベルの場所を見つけることができます。 2番目の出力は、ラベルのインデックスを提供します。結果を表示するには、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 
関連する問題