필터 지우기
필터 지우기

How do I do Pyramid plot

조회 수: 6 (최근 30일)
Diogo Costa
Diogo Costa 2020년 5월 4일
편집: Bjorn Gustavsson 2020년 5월 5일
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)

채택된 답변

Image Analyst
Image Analyst 2020년 5월 4일
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
Diogo Costa
Diogo Costa 2020년 5월 5일
Thank you so much, you were so helpfull :))
Bjorn Gustavsson
Bjorn Gustavsson 2020년 5월 5일
편집: Bjorn Gustavsson 2020년 5월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 5월 4일
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
Diogo Costa
Diogo Costa 2020년 5월 4일
편집: Image Analyst 2020년 5월 4일
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 2020년 5월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by