How to call index of vector in matrix?

조회 수: 10 (최근 30일)
ha ha
ha ha 2017년 8월 27일
댓글: ha ha 2017년 10월 27일
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.
ha ha
ha ha 2017년 10월 27일
Thanks.ok

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

추가 답변 (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!