简体   繁体   中英

How to find a value of one vector, in a range of value in another vector in matlab

I have a vector A with size of 54000 x 1 and vector B with size of 54000 x 1 which is standard deviation of elements of A. From the other side, I have vector C with size of 300000 x 1. Now I want to find that each element of vector C correspondences to which row of vector A with accepted range 3*standard deviation? I have written the below code and it works fine for small vector but for large vector such that I have it is too too slow!!

    for i=1:length(A)
      L=A(i,1)- 3*B(i,1);
      U=A(i,1)+ 3*B(i,1);
      inds{i,1} = not(abs(sign(sign(L - C) + sign(U - C))));
     end

Does anybody know how can I make this code faster or does anybody know another solution? THX.

MATLAB is an acronym for Matrix Laboratory and was developed to simply and speed up matrix(vector) calculations. Compared to C or any other programming language you can often skip the for loops when working with matrices. For your code you should be able to skip the for loop and do it as this:

  L = A - 3*STD;
  U = A + 3*STD;
  inds = not(abs(sign(sign(L - C) + sign(U - C))));

And remember that i means the complex number in Matlab. Don't know if it affects speed though.

Edit:

Getting the result as a cell:

inds = num2cell(inds)

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