Find the values ​​in the matrix and put them in the vector

It should be simple, but surprisingly, I could not find the answer to this problem here or through trial and error.
I want to get the values ​​from the matrix (according to some condition) and put the values ​​in the vector. I also need indexes of indices of the corresponding values. There is a lot of data, so no for loops.

This is the correct (but iterative) answer:

[I,J] = find(A > 5);
values = zeros(numel(I),1);
for i=1:numel(I)
    values(i) = A(I(i),J(i));
end

I tried values = A(I,J), but it is n-by-n instead of n-by-1.

+3
source share
1 answer

You can implicitly treat the matrix as a vector ( linear indexing ):

I = find(A > 5);
values = A(I);

, :

values = A(A > 5);
+7

Source: https://habr.com/ru/post/1770017/


All Articles