How to call index of vector in matrix?

90 visualizzazioni (ultimi 30 giorni)
ha ha
ha ha il 27 Ago 2017
Commentato: ha ha il 27 Ott 2017
Hello, I have: n-by-3 matrix A, and n-by-1 matrix B:
A=[ x1 y1 z1
x2 y2 z2
x3 y3 z3
x4 y4 z4
.......
xn yn zn ]
B=[ 3
2
7
1
...
n ]
B is index (labelling) matrix of A.
I wanna assign the vector A to vector B.
Ex:
(x1 y1 z1) assign to 1
(x3 y3 z3) assign to 3
......................
(xn yn zn) assign to n
- Instead of working with matrix A, I can work with "labelling" matrix B.
and then,
+ when I call 3 in matrix B, it will show the value (x3 y3 z3) from matrix A.
+ when I call 7 in matrix B, it will show the value (x7 y7 z7) from matrix A.
+ when I call [3,7] in matrix B, it will show the value matrix:
A=[ x3 y3 z3
x7 y7 z7]
+ when I call n in matrix B, it will show the value (xn yn zn) from matrix A. ...............................................................
and vice versa ( I call (x3 y3 z3) from A, it will show 3 in B.....)
- How to write a code to call matrix A from matrix B , and vice versa?
(the number 3,2,7,1,....n: in matrix B is are arbitrary numbers and x1,x2,x3...xn : are the number)

Risposta accettata

Image Analyst
Image Analyst il 27 Ago 2017
I don't know what it means to "call label 3" or B. Is B always 1,2,3,4 etc. - the natural counting numbers? Or can they be arbitrary numbers? If you have a number 3, and want to look up B(3) and use that as an index to get that row of A, you can simply do
out = A(B(3), :);
and if you want to "show" it, you can just leave off the semicolon:
out = A(B(3), :)
or use fprintf() :
fprintf('A(%d) = [%f, %f, %f]\n', B(3), A(B(3), 1), A(B(3), 2), A(B(3), 3));
or msgbox() or helpdlg():
message = sprintf('A(%d) = [%f, %f, %f]\n', B(3), A(B(3), 1), A(B(3), 2), A(B(3), 3));
uiwait(helpdlg(message));
Is that what you want?
  6 Commenti
Jan
Jan il 27 Ott 2017
@ha ha: Please use flags only to inform admins and editors about messages, which conflict with the terms of use of this forum. Thanks.

Accedi per commentare.

Più risposte (1)

Andrei Bobrov
Andrei Bobrov il 27 Ago 2017
Modificato: Andrei Bobrov il 27 Ago 2017
A = [...
6 -3 4
7 8 -5
-4 8 6
7 1 8
3 6 4
-4 -4 5
-2 0 5
2 7 0
8 6 4
8 8 -3];
A2B(A,[6 -3 4;3 6 4])
A2B(A,[1;2])
here function A2B:
function out = A2B(A,in)
if size(in,2) == 1
out = A(in,:);
else
out = find(ismember(A,in,'rows'));
end
end
  1 Commento
ha ha
ha ha il 28 Ago 2017
Modificato: ha ha il 29 Ago 2017
ok. your answer is also a solution. Thanks
index_matrix = find(ismember(A,B,'rows'));
This fuction is to find "index matrix" of each vector in matrix B from matrix A

Accedi per commentare.

Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!