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 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 9 Okt. 2018
Bearbeitet: Stephen23 am 9 Okt. 2018
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 8 Okt. 2018
Bearbeitet: Stephen23 am 8 Okt. 2018
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
____________
/ / /|
/ / / |
/_____/_____/ |
| | | /
| | | /
|_____|_____|/

Weitere Antworten (1)

KSSV
KSSV am 8 Okt. 2018
You need to plot a cube....have a look on this file exchange function: https://in.mathworks.com/matlabcentral/fileexchange/15161-plotcube
  3 Kommentare
John Drake
John Drake am 8 Okt. 2018
So after getting the input , i have to create txt file with cubes only using symbols '/', '_', '|'. I hope you got that! Please help!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by