How to display stacking circle

1 回表示 (過去 30 日間)
Shiyun Li
Shiyun Li 2020 年 5 月 21 日
回答済み: Walter Roberson 2020 年 5 月 22 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by