How to call index of vector in matrix?

90 Ansichten (letzte 30 Tage)
ha ha
ha ha am 27 Aug. 2017
Kommentiert: ha ha am 27 Okt. 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)

Akzeptierte Antwort

Image Analyst
Image Analyst am 27 Aug. 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 Kommentare
Jan
Jan am 27 Okt. 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.
ha ha
ha ha am 27 Okt. 2017
Thanks.ok

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 27 Aug. 2017
Bearbeitet: Andrei Bobrov am 27 Aug. 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 Kommentar
ha ha
ha ha am 28 Aug. 2017
Bearbeitet: ha ha am 29 Aug. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!