How to compare 3 different arrays
4 次查看(过去 30 天)
显示 更早的评论
Dear All,
I have 3 arrays as shown below (A, L1, E), First, I need to check array A, in each column the similar number and then take the location of each similar number. Then, when I found there are number same in each column , I move to array L1 and compare to locations of the similar number and take the highest location number = 1. In case if the locations in array L1 same as well, then I need to go to the array E and compare the locations of highest number and give it value 1.
A=[22 40 55 72;
33 55 32 67;
45 40 23 65;
22 40 55 45
];
L1=[3 5 60 3;
2 7 6 43;
5 5 45 56;
3 5 55 34
];
E=[0.52 0.3 0.7 0.45;
0.2 0.11 0.6 0.2;
0.1 0.32 0.45 0.33;
0.51 0.31 0.79 0.2
];
the expected results should be:
matrix=[1 0 0 1;
1 1 1 1;
1 1 1 1;
0 1 1 1
];
Help please , I have stuck with this problem since week. Please admin, I appreciate if you dont locked the question, its not part of assignment.
1 个评论
Walter Roberson
2018-3-7
In column 2, the 55 in the second row is unique, so matrix(2,2) = 1.
In column 2, the 40 in rows 1, 3, 4 are the same, so for those we have to look in column 2 row 1, 3, 4 of L1. We see 5 for all of those entries there, so we have to look in E. The corresponding entries in E are 0.3, 0.32, 0.31, the highest of which is 0.32 from row 3. So matrix(2,3) = 1. But that means that neither (2,1) nor (2,4) are highest under those rules, so matrix(2,1)=0 and matrix(2,4)=0.
That would leave us with matrix(:,2) = [0; 1; 1; 0]. But your requested output is [0; 1; 1; 1] there.
Did I misunderstand the rules, or is your expected result incorrect?
回答(2 个)
skysky2000
2018-3-28
1 个评论
Walter Roberson
2018-3-28
Just write a loop. It isn't worth spending weeks to get a nice vectorized expression. Get your code working first and then worry about vectorizing.
另请参阅
类别
在 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!