Search rows of a matrix on another
1 view (last 30 days)
I have two matricies. The first, A, is large (1968x100 double) and the second, T, is smaller (1968x1). In matrix A, each row contains unique elements corresponding to the smaller matrix. For example, A(1,:) = 6 2 3 53..... (up until 100 columns). What I need to do is search in T for the corresponding ROWS and extract their numeric value. So for this example, I need to search in T for the 6th, 2nd, 3rd, 53rd rows and extract their values.
I hope this is clear !!! Any help is much appreciated.
Guillaume on 16 Nov 2015
Edited: Guillaume on 16 Nov 2015
There is no search involved. It's simple matrix indexing:
A = [2 3 5;
1 2 4;
7 8 10];
T = [200; 500; 750; 204; 567; 123; 456; 789; 987; 654; 321];
newA = A; newA(A == 0) = 1; %to cope with zeros
values = T(newA); %that's it!
values(A == 0) = 0 %assuming that you want 0 for A == 0
Note that the shape of T is irrelevant as long as A is a matrix. T can be a column or row vector (or even a matrix).
edited to cope with 0s in A