How to map points in Vector A with points in vector B?

조회 수: 2 (최근 30일)
Rayan Glus
Rayan Glus 2021년 3월 10일
댓글: Rayan Glus 2021년 3월 13일
Hi,
I have two row vectors with the same size.
A = randi(100,1,100);
B = randi([101, 200],1,100);
I want to have another row vector AB where :
AB(i) = j ;% i and j are elements in A and B respectively
AB(j) = i;
AB's length supposed to be length of A + length of B, correct? What if in one case i and j happens to have the same value when both A and B are :
A = randi(100,1,100);
B = randi(100,1,100);
Thanks!
  댓글 수: 2
Jorg Woehl
Jorg Woehl 2021년 3월 10일
편집: Jorg Woehl 2021년 3월 10일
Rayan, can you clarify what you mean with AB(i)=j and AB(j)=i where "i and j are elements in A and B respectively"? Or, more precisely, what is the link between i and j?
Taking a simple example:
A = randi(100,1,5)
A =
21 31 48 24 85
B = randi([101, 200],1,5)
B =
120 123 118 123 144
Do you now want AB(21)=120, and AB(120)=21, meaning that i and j are the corresponding elements in A and B? Or, expressed mathematically: i=A(k) and j=B(k) for a given k?
If that's the case, the vector AB cannot be constructed, because several elements of A can have the same value, let's say "19". That means that A(19) cannot be uniquely defined. And the same argument holds for vector B. (I think this is the question that you were pondering,too.)
Rayan Glus
Rayan Glus 2021년 3월 10일
Thanks for your reply Jorg.
So, A and B are basically, vectors of cities. And I want to map between these cities. Let's say that city 21 in A is mapped to city 120 in B and vice versa.
If that's the case, the vector AB cannot be constructed.
I agree with you. But what if we define:
A = 1:100;
B = 101:200;
Would it be possible then to map between the i's and j's?

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

채택된 답변

Steven Lord
Steven Lord 2021년 3월 10일
So you want something like this?
v = randperm(10) % Shuffle the numbers from 1 to 10
v = 1×10
7 3 9 4 6 1 10 2 5 8
A = v(1:5) % Cut the "deck"
A = 1×5
7 3 9 4 6
B = v(6:10)
B = 1×5
1 10 2 5 8
AB = zeros(1, 10);
AB(A) = B;
AB(B) = A
AB = 1×10
7 9 10 5 4 8 1 6 2 3
  댓글 수: 2
Rayan Glus
Rayan Glus 2021년 3월 10일
Yes, sir!
This is exactly what I'm looking for.
Thank you so much.
I really appreciate it.
Rayan Glus
Rayan Glus 2021년 3월 13일
Hi Steven,
Is it possible to map one to many? I apologize if it's a stupid question and undoable.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by