How do I do Pyramid plot

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

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

I can´t figure it out, I tried but i can´t achieve what it supposed to achieved
Take it step-by-step. How did you produce the figure in the image above? Did you manage to make one in the y-direction?
I produced this code to create the image above
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)
Well, I see you totally ignored my advice. Why??? Where is your loop over rows, and how does the M value depend on the row? Please take my advice and you will have your homework problem solved. I assume you're not allowed to turn in someone else's solution as your own.
No, i didn't try that way, i'm working on that now. I understand i need a certain rows to be white but i can figure it out the numbers of the matrix to make a diagonal line :/
What if you do that "if" block you did for col just after the row for loop and before the col for loop starts? And if you let M be a double to start with, instead of a uint8? Get a row value and a col value. The bottom part of the code would then look like:
finalValue = colValue * rowValue;
M(row, col) = finalValue;
end
end
hFig = figure;
% Now convert the double M to uint8
M = uint8(255 * mat2gray(M));
imshow(M, []);
impixelinfo
hFig.WindowState = 'maximized'
I add this
if row < 257
valor = row-1;
else
valor = rows - row;
end
and did this to the image
I'm closer i know
clear all; close all; clc;
M = zeros(512,512);
[rows, colunas] = size(M);
for row = 1 : rows
for col = 1 : colunas
if col < 257
colvalor = col-1;
else
colvalor = colunas-col;
end
if row < 257
rowvalor = row-1;
else
rowvalor = rows - row;
end
finalvalor = colvalor * rowvalor;
M(row, col) = finalvalor;
end
end
hFig = figure;
M = uint8(255 * mat2gray(M));
imshow(M, []);
impixelinfo
hFig.WindowState = 'maximized'
i need that to be a cross X not a plus sign + :(
I'm portuguese btw, sry for the language on the code
Right, but I'm sure you'll figure it out - you're a smart engineer.
I managed to do this, any final tip ? I'm not that good programming :((
clear all; close all; clc;
M = zeros(512,512);
[rows, colunas] = size(M);
for row = 1 : rows
for col = 1 : colunas
if col < 257
valorcol = col - 1;
else
valorcol = colunas - col;
end
if row < 257
rowvalor = row - 1;
else
rowvalor = rows - row;
end
valorfinal = valorcol + rowvalor;
M(row, col) = valorfinal;
end
end
hFig = figure;
M = uint8(255 * mat2gray(M));
imshow(M, []);
impixelinfo
hFig.WindowState = 'maximized';
You almost had it but you need to use min() instead of plus when you compute valorfinal:
M = zeros(512,512);
[rows, colunas] = size(M);
for row = 1 : rows
% Get the row value.
if row < 257
rowvalor = row - 1;
else
rowvalor = rows - row;
end
for col = 1 : colunas
% Get the column value.
if col < 257
valorcol = col - 1;
else
valorcol = colunas - col;
end
% Make M the minimum.
valorfinal = min(valorcol, rowvalor);
M(row, col) = valorfinal;
end
end
hFig = figure;
M = uint8(255 * mat2gray(M));
imshow(M, []);
impixelinfo
hFig.WindowState = 'maximized';
Thank you so much, you were so helpfull :))
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)

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

Community Treasure Hunt

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

Start Hunting!