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.

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 commentaires

What are the first and last rows for?
So first row is "behind", last row is "in front"?
@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.

Connectez-vous pour commenter.

 Réponse acceptée

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
____________
/ / /|
/ / / |
/_____/_____/ |
| | | /
| | | /
|_____|_____|/

Plus de réponses (1)

You need to plot a cube....have a look on this file exchange function: https://in.mathworks.com/matlabcentral/fileexchange/15161-plotcube

3 commentaires

Not ploting, drawing a cubes in a txt file, that's the problem
Ohhh.....yes it is mentioned a text file..why text file in specific?
So after getting the input , i have to create txt file with cubes only using symbols '/', '_', '|'. I hope you got that! Please help!

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by