MATLAB Answers


Access matrix location on one matrix with the data location on another matrix

Asked by Rohan Mehta on 21 Jun 2019 at 8:32
Latest activity Edited by madhan ravi
on 21 Jun 2019 at 9:16
I have two matrix say A & B if I have data stored in A and the data location of desired values in B how could I access Values of matrix A who are located at locations B.


Sign in to comment.

2 Answers

Answer by Chinmay Anand on 21 Jun 2019 at 8:40
 Accepted Answer

In case if A and B are vectors ,( 1 x n ) matrices
A(B) % It will give the values of A at indexes B


I want to keep values of A on location B as it is and set all other to zero.
Thanks for the help....
idx = setdiff(1:length(A),B); % indexes which are not in B
A(idx) = 0;

Sign in to comment.

Answer by madhan ravi
on 21 Jun 2019 at 9:02

Probably you want:
idx = setdiff(1:numel(A),B);
A(idx) = 0;


Beware this method works even if A is a matrix more than 1 dimension unlike the other.

Sign in to comment.