MATLAB: Ploting a 2D matrix and adjusting axes

2d matrix3d plotsaxisaxis adjustimagescplot 2d matrixsurf 3d

Hello, I have the following code:
M = zeros(50)
for x = [20,30]
M(x,:) = 1
end
for y = [13,25,37]
for x = [1:20, 30:50]
M(x,y) = 1
end
end
imagesc(M)
when i get the plot, i see the y-axis is in reverse order i-e starting from 50 at the bottom. In my overall simulation this affects my results. You can see the attached picture .Figure 1 is what i want and figure 2 is what this code gives me. I need to know how can i get.

Best Answer

This thing worked for me:
clc
clear all
close all
M = zeros(50);
% draw lines
for y = [20,30]
M(:,y) = 1;
end
for x = [13,25,37]
for y = [1:20, 30:50]
M(x,y) = 1;
end
end
p1 = [12,48];
p2 = [39,5];
% get direction
d = p1-p2;
d = d/max(abs(d)); % limit to 1 px step size
steps = 0:1:max(abs(p2-p1));
for i=length(p1):-1:1
p_line(:,i) = round(p2(i) + steps.*d(i));
end
idx = sub2ind(size(M), p_line(:,1), p_line(:,2));
walls = sum(M(idx));
M(idx) = 2;
M = flipud(M)
M = rot90(M,-1)
imagesc(M)
set(gca,'YDir','normal')
set(gca,'XDir','normal')
fprintf('You passed %i walls', walls)