MATLAB Answers

Illustrate weighted matrix to represents areas

11 views (last 30 days)
Naza Karim
Naza Karim on 18 Dec 2019
Commented: Naza Karim on 3 Feb 2020 at 12:25
Hi
I am trying to illustrate a square matrix (weights) that represents sqare area that's divided into smaller squares. The values of the 'wiegth' matrix are ones or zeros.
I also have a position vector that has the positions of 2 agents for example within this area (squares are numbered as in first figure).
I want to draw the area while showing the squares that have wights 0f 1, and mark the positions of the agents (something similar to the next figure).
AreaBlocks=36;
NoOfAgents=2;
wieghts=randi(2,sqrt(AreaBlocks))-1;
position=randi(AreaBlocks,[1 NoOfAgents]);
% example:
wieghts=[0 0 0 0 0 0;
0 0 0 1 0 0;
0 1 1 1 0 0;
0 0 1 1 1 0;
0 1 1 1 0 0;
0 0 0 0 0 0];
position=[21 17];
22.png 11.png
I want to create a function that draws this area with adjustable AreaBlocks and NoOfAgents. Any help or ideas on how to do so?
Thank you

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 18 Dec 2019
Edited: Guillaume on 18 Dec 2019
You can do something like:
weights = [0 0 0 0 0 0; ...corrected spelling of the variable
0 0 0 1 0 0;
0 1 1 1 0 0;
0 0 1 1 1 0;
0 1 1 1 0 0;
0 0 0 0 0 0];
position = [21 17];
hfig = figure;
hax = gca;
%plot matrix and customise display
imagesc(hax, weights);
colormap(hax, [1 1 1; 1 .8 0]); %white and yellowship colour map
hax.XTick = 0.5:size(weights, 2)+0.5; %move ticks to 'edges' of squares
hax.YTick = 0.5:size(weights, 1)+0.5;
grid(hax, 'on'); %display grid at tick marks
hax.GridAlpha = 1; %with no transparency
hax.XAxis.Visible = false; %hide axis
hax.YAxis.Visible = false;
axis(hax, 'square');
%plot position
hold(hax, 'on');
[y, x] = ind2sub(size(weights), position);
hplot = plot(hax, x, y, 'Marker', 'o', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r', 'MarkerSize', 12, 'LineStyle', 'none');

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.