How can i write the function which input is a 3×3 matrix, named A, and the program should output corresponding picture in a txt file. The number in matrix A determines how many cubes should be located in the corresponding position.
조회 수: 32 (최근 30일)
이전 댓글 표시
How can I write the function which input is a 3×3 matrix, named A, and the program should output the corresponding picture in a txt file. The number in matrix A determines how many cubes should be located in the corresponding position. Here are some samples in picture

댓글 수: 4
Stephen23
2018년 10월 9일
편집: Stephen23
2018년 10월 9일
@John Drake: do not close question that have answers. This is a public forum, and the volunteers who answer questions here do so on the understanding that their answer will be available for all future browsers of this forum. By deleting your question text and closing the question you unilaterally decide that their volunteer effort and time belongs only to you. This is not appreciated.
If you want private consulting then you can find plenty of services on the internet who will happily give you code and help, in return for a small fee.
채택된 답변
Stephen23
2018년 10월 8일
편집: Stephen23
2018년 10월 8일
Here is one approach using simple loops and indexing.
You will have to do some fine-tuning to get it to suit your requirements.
A = [0,0,0;0,2,1;0,1,0]; % Input 3x3 matrix.
%
M = [... % base cube
'~~~______~';
'~~/ /|';
'~/ / |';
'/_____/ |';
'| | /';
'| | /~';
'|_____|/~~'];
%
Sa = size(A);
Sm = size(M);
Xm = M~='~';
Xr = cell(Sa);
Xc = cell(Sa);
% Generate indices:
for ii = 1:Sa(1) % back -> front.
for jj = 1:Sa(2) % left -> right.
for kk = 1:A(ii,jj) % bottom -> top.
R = ii*3 - kk*3;
C = jj*6 - ii*3 - kk; % try without kk.
Xr{ii,jj} = [Xr{ii,jj};R+1-Sm(1):R];
Xc{ii,jj} = [Xc{ii,jj};C+1-Sm(2):C];
end
end
end
% Normalize indices:
Xr = vertcat(Xr{:});
Xc = vertcat(Xc{:});
Xr = 1+Xr-min(Xr(:));
Xc = 1+Xc-min(Xc(:));
% Place cube at required locations:
Z = repmat(' ',max(Xr(:)),max(Xc(:)));
for nn = 1:sum(A(:))
tmp = Z(Xr(nn,:),Xc(nn,:));
tmp(Xm) = M(Xm); % keep char outside the cube.
Z(Xr(nn,:),Xc(nn,:)) = tmp;
end
disp(Z)
Displays this in the command window:
______
/ /|
/ / |
/_____/ |______
| | / /|
| | / / |
|______/_____/ |
/ /| | /
/ / | | /
/_____/ |_____|/
| | /
| | /
|_____|/
Tested with the first example input matrix:
>> A = [0,0,0;0,1,1;0,0,0]
A =
0 0 0
0 1 1
0 0 0
Giving
____________
/ / /|
/ / / |
/_____/_____/ |
| | | /
| | | /
|_____|_____|/
댓글 수: 0
추가 답변 (1개)
KSSV
2018년 10월 8일
You need to plot a cube....have a look on this file exchange function: https://in.mathworks.com/matlabcentral/fileexchange/15161-plotcube
댓글 수: 3
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!