How do I do Pyramid plot
조회 수: 6 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
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
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
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
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 Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!