How do I do Pyramid plot

12 views (last 30 days)
Diogo Costa
Diogo Costa on 4 May 2020
Edited: Bjorn Gustavsson on 5 May 2020
So I did this code, it shows a plot that looks like a roof with the colors black and white.
Now I need help with a similar code but that shows a pyramid, not a 3D pyramid. I put an attachment for you guys to see what I'm looking for. Thanks :))
M=uint8(zeros(512,512));
[r,c]=size(M);
for j=1:c
if j < 257
M(:,j)=j-1;
else
M(:,j)=c-j;
end
end
figure, imshow(M)

Accepted Answer

Image Analyst
Image Analyst on 4 May 2020
What about using row? Your code only checks the column, so of course every row is the same. You need to consider the row. Have a loop over the rows and use the row to compute the true value. Here is a snippet to get you started:
M = zeros(512,512, 'uint8');
[rows, columns] = size(M);
for row = 1 : rows
for col = 1 : columns
% You need to fix the lines below to set the value properly.
% Just think hard enough about it and you'll figure it out.
if col < 257
value = col-1; % What about row. It needs to be involved!
else
value = columns-col;
end
M(row, col) = value;
end
end
hFig = figure;
imshow(M, []);
hFig.WindowState = 'maximized'
  15 Comments
Diogo Costa
Diogo Costa on 5 May 2020
Thank you so much, you were so helpfull :))
Bjorn Gustavsson
Bjorn Gustavsson on 5 May 2020
Edited: Bjorn Gustavsson on 5 May 2020
Now that you have an iterative solution you might also take a look at this matrix-based version:
x = -21:21;
[x,y] = meshgrid(x,x);
imagesc(min(21-abs(x),21-abs(y)))
% or for a +-signed roof:
imagesc(min(21-abs(x+y),21-abs(x-y)))
At some stage you should start thinking about solving the problems with matrix-based operations too - that is one of the strengths of matlab.

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 4 May 2020
Surely this is a homework task?
Regardless, after you've figured out loops and conditional statements, you should start to think about more matrix based operations.
For example if you want to assign one value to every element in a column of a matrix you can do that in one step:
idx_col2fill = 23;
M = ones(512);
M(:,idx_col2fill) = 0;
Then you should have a look at special matrices that can be used for a range of purposes. Check for example diag:
imagesc(diag(32))
and simple matrix-manipulation operations such as flipud, fliplr, and the transpose operators: ' and .'
HTH
  2 Comments
Diogo Costa
Diogo Costa on 4 May 2020
Edited: Image Analyst on 4 May 2020
Ye it is, i'm searching for some help :((
It need to seem like this:
but instead of that it needs to be a cross that looks like a pyramid seen from above
Bjorn Gustavsson
Bjorn Gustavsson on 4 May 2020
But now you're well on your way! Take pen and paper, figure out how you can combine this variation in the x-direction with a similar variation in the y-direction, and when you've at least figured out how to do the same shape in the y-direction you should take a good look at the min and max functions.
HTH

Sign in to comment.

Categories

Find more on Line 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!