필터 지우기
필터 지우기

Search rows of a matrix on another

조회 수: 1 (최근 30일)
Matlab User
Matlab User 2015년 11월 16일
댓글: Matlab User 2015년 11월 16일
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.
  댓글 수: 2
Jan
Jan 2015년 11월 16일
편집: Jan 2015년 11월 16일
A small example with [3 x 4] matrices might explain the problem uniquely.
Do you really want to search the "corresponding rows in T", when T has one row only? Do you mean searching the rows in A instead?
Matlab User
Matlab User 2015년 11월 16일
For each row in A there are 100 elements. T has 1968 rows and 1 column. So, say for example A=[2 3 5;1 2 4;7 8 10], and T=[200;500;750,204,567..... up to 1968 rows], for A(1,:) I want to report the value of the 2nd, 3rd and 5th rows in T, i.e. 500,750 and 567. I hope this is clearer.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 11월 16일
편집: Guillaume 2015년 11월 16일
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
  댓글 수: 3
Guillaume
Guillaume 2015년 11월 16일
편집: Guillaume 2015년 11월 16일
The zeros are indeed a problem. What is supposed to be returned for the zeros?
I've edited my answer to output 0 for these.
Matlab User
Matlab User 2015년 11월 16일
Thankyou that is just what I needed!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by