How to illustrate a cell array

5 ビュー (過去 30 日間)
Naza Karim
Naza Karim 2019 年 7 月 16 日
コメント済み: Naza Karim 2019 年 7 月 17 日
I have the following cel array:
x{1}=[0 0 0 0];
x{2}=[0 0 -1 0 0];
x{3}=[0 0 0 1 1 0];
x{4}=[0 0 1 1 0];
x{5}=[-1 -1 0 -1];
Which supposed to represent the state of Abalone game board, like this:
abalone4.png
Where zeros mean empty cell, -1's mean occupied by green marble, and 1's occupied by blues.
Is there any possible way to illustrate the board using my cell array?
  5 件のコメント
Naza Karim
Naza Karim 2019 年 7 月 16 日
I am aware. I just have no idea if there is any possible way to draw such a customised figure. Thank you though for pointing to viscircles function.
Joel Handy
Joel Handy 2019 年 7 月 16 日
編集済み: Joel Handy 2019 年 7 月 16 日
It can definitely be done. viscircles should let you create a figure with all of the circles. The fill function will let you plot the hexagon. Placing all of the shapes should be inde[endant of X. all you need to do while plotting the board is refer to your cell array to choose colors.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 7 月 16 日
編集済み: Stephen23 2019 年 7 月 16 日
There are no doubt many ways of doing this, one relatively intuitive way would be to use scatter. This should get you started (but it is not intended to be final code):
x{1} = [0,0,0,0];
x{2} = [0,0,-1,0,0];
x{3} = [0,0,0,1,1,0];
x{4} = [0,0,1,1,0];
x{5} = [-1,-1,0,-1];
% Hexagon:
Px = [-2,2,3.7,2,-2,-3.7,-2];
Py = [-6,-6,-3,0,0,-3,-6];
patch(Px,Py,0.6*[1,1,1])
hold on
% Circles:
Fx = @(v)(1:numel(v))-numel(v)/2-1/2;
Fy = @(v,n)-v*ones(1,n);
Cx = cellfun(Fx,x,'uni',0);
Cy = arrayfun(Fy,1:numel(x),cellfun('length',x),'uni',0);
Sh = scatter([Cx{:}],[Cy{:}],1024,[x{:}],'filled')
% Colors:
caxis([-1,1])
colormap([0,0.8,0;1,1,1;0.2,0.2,1])
set(gca,'visible','off')
You could also hard-code the X and Y values, which would no doubt simplify the code.
Note that you can change the colors simply by updating the scatter object's CData property (i.e. you do not need to replot everything when you change the circle colors):
  1 件のコメント
Naza Karim
Naza Karim 2019 年 7 月 17 日
Thank you! this indeed helps a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTitle についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by