0

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?

Dan
  • 45,079
  • 17
  • 88
  • 157
Acobot
  • 27
  • 6
  • i got 1. but i want to find the location – Acobot May 18 '16 at 07:41
  • I still cannot get the picture how `A` and `B` are connected and what do you mean by **true location in B=8**. There is no element in `B` equal to `8`, neither in `A`. – Crowley May 18 '16 at 08:07
  • actually this is part of the cross validation process using logical values. the count of original value is equal to the count in B. Using logical 1, only A is selected – Acobot May 19 '16 at 02:39

2 Answers2

1

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)).

patrik
  • 4,506
  • 6
  • 24
  • 48
  • dear patrik, i get error for the function idx=allIdx(C) .is it a build in function? – Acobot May 18 '16 at 07:38
  • @Acobot `allIdx` is not a function. This was meant as an example of the vector where you store the ones in B `allIdx = find(B);`. The first 2 lines will do the trick and `allIdx = find(B);` will relieve you from repeating these lines everytime you want to use the indices from B. This does somehow imply that this is some kind of model information, which does not go out of scope at the end of the function (for example that you use a struct as input and output). – patrik May 18 '16 at 07:48
  • thanx patrik, i got it – Acobot May 18 '16 at 08:01
0

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
Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19
  • Why you use `i` as counter when it is reserved for imaginary unit? – Crowley May 18 '16 at 07:50
  • @Crowley `i` or `j` or `1i` can be used for imaginary unit. That doesn't mean it can't be used. `1i` is actually reserved. – Rijul Sudhir May 18 '16 at 07:53
  • I've tried to overwrite `i` and evaluate `3+5*1i` and there's no interference. So you're right. Still I'll be careful because overwriting `i` and using complex number in form `3+5*i` will cause unexpected and hardly traceable errors. – Crowley May 18 '16 at 07:57
  • @Crowley If you use it like `3+5i`, the `i` will act as imaginary unit. But if it is `3+5*i`. Then it will be considered as a variable. So I don't think it will be an issue. – Rijul Sudhir May 18 '16 at 08:01
  • Problem is, that `i` and `j` are preallocated as imaginary units and are not protected against overwriting. `1i=5;` throws error, `i=5;` does not. Try `clear i,3+5*i,i=2;3+5*i`. – Crowley May 18 '16 at 08:48
  • @Crowley First of all you cant do this `1i=5;`. Because variable name cant start with a number. – Rijul Sudhir May 18 '16 at 08:55
  • 2
    @Crowley this discussion should be held [here](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). However the `i` and `j` are **not** reserved words. They are actually functions (try `i(), help i, edit i`) and as such they can be overridden. Anyway `1i` is reserved for the imaginary unit (note that the **1** prevents it from being overridden). Note that I am not stating whether or not to iterate with `i` and `j`. This discussion should be held in the provided link instead. – patrik May 18 '16 at 09:37