How to call index of vector in matrix?

72 次查看(过去 30 天)
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)

采纳的回答

Image Analyst
Image Analyst 2017-8-27
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 个评论
Jan
Jan 2017-10-27
@ha ha: Please use flags only to inform admins and editors about messages, which conflict with the terms of use of this forum. Thanks.

请先登录,再进行评论。

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2017-8-27
编辑:Andrei Bobrov 2017-8-27
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 个评论
ha ha
ha ha 2017-8-28
编辑:ha ha 2017-8-29
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!