52 views (last 30 days)

Show older comments

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
on 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?

Andrei Bobrov
on 27 Aug 2017

Edited: Andrei Bobrov
on 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

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

Start Hunting!