0

I am modeling a SIR disease spreading model in Matlab, I have a grid which is a cell array, and each cell mean a state (s,i,r).

I want to plot grid with the s as blue dots and i as red dots, and the axis take the 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'     []     []     []     []

1 Answer 1

2

You can use ismember to find where each label exists in your cell array. The second output will provide the index of the label. You can then use imagesc with a custom colormap to display the result.

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

And if we test this with Grid = {'s', []; 'i', []}

enter image description here

If you want actual dots instead, you can do something like this:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.