help completeing Alphabetical matrix problem

조회 수: 2 (최근 30일)
Hayden O'Neil
Hayden O'Neil 2017년 10월 7일
답변: Walter Roberson 2017년 10월 8일
Write Matlab code that creates a new 5x10 matrix that creates an alphabetical matrix that displays the letter of the matrix that has the HIGHEST value of the 3 matrices A,B,C from Problem #1 on an element by element basis
%Question 3
A = [12 3 56 78 3 4 5 10 91 21; 16 18 3 5 7 17 4 7 10 11;
2 4 6 8 10 12 10 6 3 19; 1 22 8 9 12 7 17 5 8 10;
7 6 2 1 45 8 17 8 9 16];
B = [16 81 18 3 5 7 9 12 9 11; 0 2 3 7 82 6 9 15 12 10;
7 4 1 1 8 7 15 43 11 17; 8 3 1 0 1 0 4 5 9 16;
7 6 2 1 45 8 17 8 9 16];
C = [6 7 68 24 13 2 8 16 22 2; 67 13 4 5 9 11 16 12 22 1;
13 14 15 9 64 41 12 11 10 3; 2 0 1 4 6 1 6 2 3 3;
8 5 6 4 2 5 60 25 13 3];
m = max( max (A,B), C);
vars = {'A','B','C'};
mask1 = m == A;
mask2 = m == B;
mask3 = m == C;
which_one = cell(size(m));
which_one(mask1) = vars(1);
which_one(~mask2) = vars (2);
Your output should look like this :
OutputMatrix = [B B C A C B B C A A;C A C B .. .. .. .. .. ..; .. .. .. .. .. .. .. .. .. ..;
this is what I have so far up my output matrix is incorrect.

채택된 답변

Kian Azami
Kian Azami 2017년 10월 7일
If your question is understood correctly, by the following code you can create the Alphabetical matrix that you need:
clc
clear all
A = [12 3 56 78 3 4 5 10 91 21; 16 18 3 5 7 17 4 7 10 11;...
2 4 6 8 10 12 10 6 3 19; 1 22 8 9 12 7 17 5 8 10; 7 6 2 1 45 8 17 8 9 16];
B = [16 81 18 3 5 7 9 12 9 11; 0 2 3 7 82 6 9 15 12 10;...
7 4 1 1 8 7 15 43 11 17; 8 3 1 0 1 0 4 5 9 16; 7 6 2 1 45 8 17 8 9 16];
C = [6 7 68 24 13 2 8 16 22 2; 67 13 4 5 9 11 16 12 22 1;...
13 14 15 9 64 41 12 11 10 3; 2 0 1 4 6 1 6 2 3 3; 8 5 6 4 2 5 60 25 13 3];
ABC = {};
for jj = 1:5
for hh = 1:10
X = max([A(jj,hh),B(jj,hh),C(jj,hh)]);
if X == A(jj,hh)
ABC{jj,hh} = 'A';
elseif X == B(jj,hh)
ABC{jj,hh} = 'B';
elseif X == C(jj,hh)
ABC{jj,hh} = 'C';
end
end
end
disp(ABC)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 10월 8일
As discussed in https://www.mathworks.com/matlabcentral/answers/360076-how-to-create-an-matrix-that-is-asked-for-in-the-problem#comment_490771 that required output is not possible in MATLAB, except as a string giving the appearance of the results instead of as an array.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by