0

I believe the answer to this is simple but my brain will not function.

Say I have a m x n matrix which is of type double & lets call it A. I also have a matrix B, which is m x n and is all NaN's.

I then want to find out which numbers are say equal to some number, let say 100. I can do the following,

  A_index = A == 100;

So I now have a logical array, A_index. This is all fine.

My question is how do I select the elements from A where A_index is true into matrix B?

Some made up matrices

A= [ 50 100 75 90 100; 0 50 60 30 10; 100 25 80 250 100; 5 100 0 100 90];
A_index = A == 100;
B= zeros(4,5) * NaN;
3
  • @Sardar_Usama I've just added some sample matrices Commented Nov 1, 2016 at 8:24
  • 1
    Since A and B are the same size and shape, try B(A==100) = ... (replacing the ... by whatever expression you want on the rhs). Then read the documentation on the topic of logical indexing. Commented Nov 1, 2016 at 8:28
  • 1
    Here is a link to the SO documentation : indexing in Matlab. Commented Nov 1, 2016 at 8:36

1 Answer 1

1

Something like:

A= [ 50 100 75 90 100; 0 50 60 30 10; 100 25 80 250 100; 5 100 0 100 90];
A_index = A == 100;

B= zeros(4,5) * NaN;

B(A_index) = 100

This way you will get 100 in the entries of B where A is equal to 100

See the section on logical indexing in the MATLAB docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.