match index of arrays

조회 수: 1 (최근 30일)
Tiina
Tiina 2017년 6월 10일
댓글: Tiina 2017년 6월 12일
Hi, I have a problem with matching and indexing. The problem was raised in this forum earlier and was solved. This is how it was raised
A = [9 10 11 12 13 14 15 16 17]'
B = [10 13 16; 999 900 950]'
The desired solution is
M =
9 0
10 999
11 0
12 0
13 900
14 0
15 0
16 950
17 0
This can be attained as follows
M = A*[1 0]
M(ismember(A,B(:,1)),2) = B(:,2)
My problem is exactly the same but the solution is not working. The only difference I have is the size of A and B
A is too large and is of size(A)
ans =
1035593 1
B is small and is of size(B)
ans =
72 2
Column 1 of B contains unique ids that match unique ids in A. Column 2 is the data to be matched given ids.
When I apply the solution above, I get Subscripted assignment dimension mismatch. I am not sure why this is happening, could you kindly advise or provide an alternative solution.
  댓글 수: 1
Stephen23
Stephen23 2017년 6월 10일
편집: Stephen23 2017년 6월 10일
>> M = A(:);
>> M(:,2) = 0;
>> [~,ia,ib] = intersect(A,B(:,1));
>> M(ia,2) = B(ib,2)
M =
9 0
10 999
11 0
12 0
13 900
14 0
15 0
16 950
17 0
If you are having problems than you will need to check that every value in column one of B exists once in columns one of A.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 10일
You would get that error if there are columns in B that are not matched in A.
[~, ia, ib] = intersect(A, B(:,1));
M(ia,:) = B(ib,:);
  댓글 수: 7
Walter Roberson
Walter Roberson 2017년 6월 11일
I loaded your data and then did
M = A*[1 0];
[~, ia, ib] = intersect(A, B(:,1));
M(ia,:) = B(ib,:);
there were no error messages.
Tiina
Tiina 2017년 6월 12일
I restarted Matlab and it works fine. Thank you.

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

추가 답변 (0개)

카테고리

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