How to display stacking circle

1 次查看(过去 30 天)
Shiyun Li
Shiyun Li 2020-5-21
Hi I want to display a stacking circle according to their position in the array and the value in that position will be the number of stacking circle.
for example: if in the 3th place in the array has number 4, it will display 4 circle from the bottom.
Here is my code, I have difficulity on using "circle" so I try to solve the problem use 1 and 0. If you know how to use "circle" in this case please advice.
And What would be the better way to empty the unessary entries in the matrix? (in this case, "1" in the display solution).
% stacking circle
clear;clc;
%initialise an array represent the number of circle in each index position
array=[1 2 3 4 7 1 5 3 1 9 4 6 7 2];
%initialise the maximum value in the array
maxn=max(array);
%creates a ones matrix
stack=ones(maxn,length(array));
%loop through the index in the array
for i=1:length(array)
if array(i)==1
stack(maxn,i)=0;
else
stack(maxn-(array(i)-1):maxn,i)=0;
end
end
disp(stack);
Here is what I had in the command windows, "1" are unessccary.
1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 0 1 1 1 1 0 1 1 0 1
1 1 1 1 0 1 1 1 1 0 1 0 0 1
1 1 1 1 0 1 0 1 1 0 1 0 0 1
1 1 1 0 0 1 0 1 1 0 0 0 0 1
1 1 0 0 0 1 0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
  2 个评论
Geoff Hayes
Geoff Hayes 2020-5-22
Shiyun - can you provide a picture of what you would like to show? It isn't clear to me what you mean by if in the 3th place in the array has number 4, it will display 4 circle from the bottom.
Shiyun Li
Shiyun Li 2020-5-22
yes, sorry for the poor decription. here is an example. thank you for replying Geoff

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-5-22
stack = char(' ' + zeros(max(array),length(array)));
stack(ndgrid(max(array):-1:1) <= array) = 'o';

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by