简体   繁体   中英

How to find value from matrix

Let say I have a matrix

A=[0.8 0.9 0.7 0.5 0.3 0.8 0.2 0.1];    % 8 points

where A come from logical 1 from B

B=[1 0 1 0 0 1 0 1 0 1 1 0 1 1];

As I want to find location C that satisfies

C=find(A<0.6 & A>0.35)

where the ans is C=4 . My question is how to get the true location in B=8 ?

Unless you do not have the indices stored away somewhere, I cannot see that you have much of a choice here.

tmp = find(B);
idx = tmp(C);

In case you actually want to use this mapping more than once, I would suggest that you store the indices instead of a binary vector. This will also be more memory efficient in case the binary vector is sparse (or not a boolean vector), since you will need less entries.

In case you also need the binary vector, you should store both in case memory allows. When I have done this kind of mapping in Matlab I have actually used both a binary vector (a mask) and an index vector. This have saved me from first mapping the mask to index and then index to filtered position (so to say, skipping the tmp = find(B); idx = tmp(C); part every time and go directly to idx = allIdx(C) ).

This will get you the index in B

A=[0.8 0.9 0.7 0.5 0.3 0.8 0.2 0.1];
B=[1 0 1 0 0 1 0 1 0 1 1 0 1 1];
C=find(A<0.6 & A>0.35);
temp=0;
for i=1:size(B,2)
    temp=temp+B(i);
    if(temp==C)
        break;
    end
end
locationB=i;
locationB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM