How do I do Pyramid plot
10 次查看(过去 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!