How can I populate a matrix of zeros, with a value of 1 at specific rows and columns?

1 view (last 30 days)
I created a for loop that runs through rows and columns of a matrix and adds the x positions where the conditions are met to a new vector x and the y positions where the conditions are met to a new vector y. This vector gives me x and y coordinates that I want to plot. I now want to use these x and y positions to populate an mxn matrix with the value 1, for each of these x,y positions. How would I do this?
  3 Comments
Simi Serfati
Simi Serfati on 2 Dec 2018
I have this following code:
pacman_matrix = randi([0 1],10,10);
x = [];
y = [];
[row,col] = size(pacman_matrix);
for i = 1:row
for k = 1:col
if pacman_matrix(i,k) == 0
x(end+1) = i;
y(end+1) = k;
end
end
end
plot(x,y,'o')
I now want to use the x and y vectors created from this code (that should give me x and y coordinates where this condition is met) in order to populate a matrix that is
pacman = zeros(1328,1201)
I want for there to be a 1, wherever there is an x and y coordinate gained from the x and y vectors aqcuired beforehand.
For example: if I get that there is a zero in the pacman_matrix, whenever x = 3 and y = 2 (which would signifiy the row and col) I want for there to be a 1 in position (3,2) in the pacman matrix.
Matt J
Matt J on 2 Dec 2018
Simi Srfati's comment moved here:
I am basically trying to simulate the idea that when pacman "eats" one of the dots.
My plan to do this was to plot the dots in all of the positions that pacman is allowed to move in the game board. If the position of the pacman is equal to the position of where the dot is plotted, then the dot should dissapear. (Basically by setting the x and y positions at that index to an empty set, and then updating the x and y values on plot using set(x,'XData',y, 'YData'). I am just stuck on how I can see if the position of the pacman is the same as the position of the dot.

Sign in to comment.

Answers (2)

Matt J
Matt J on 2 Dec 2018
Edited: Matt J on 2 Dec 2018
matrix = sparse(x,y,1,m,n)

Matt J
Matt J on 2 Dec 2018
Edited: Matt J on 2 Dec 2018
pacman=~pacman_matrix;

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!