Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the inverse of find() function in matlab

In Matlab I can find all non zero entries in a vector like this:

>> v = [0 1 0 0 1]

v =

     0     1     0     0     1

>> indices = find(v)

indices =

     2     5

Assuming that my vector v can only have 0 and 1 values, what is a simple way to reproduce v from the indices vector?

like image 919
benathon Avatar asked Dec 12 '22 03:12

benathon


1 Answers

you have to know what the shape v is (i.e. how long v is if it's a vector as in your example), but once you know that it's trivial:

n = 5;
v_reconstructed = zeros(1, n);
v_reconstructed(indices) = 1;

if you don't know how long v is then you won't capture any 0s after the last 1 in v...

BTW if you are working with sparse matrices then you might want this actually:

v = sparse([0 1 0 0 1]);
v_reconstructed = full(v);
like image 61
Dan Avatar answered Dec 27 '22 03:12

Dan