简体   繁体   English

如何从矩阵中寻找价值

[英]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 其中A来自B逻辑1

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

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

where the ans is C=4 . 其中ans为C=4 My question is how to get the true location in B=8 ? 我的问题是如何获取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. 当我在Matlab中完成这种映射时,实际上我既使用了二进制矢量(掩码)又使用了索引矢量。 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) ). 这使我免于首先将掩码映射到索引,然后将索引映射到过滤后的位置(也就是说,每次跳过tmp = find(B); idx = tmp(C);一部分,直接进入idx = allIdx(C) )。

This will get you the index in B 这将使您获得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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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