How to use one matrix to constrain another matrix of the same size?

I am doing pairwise correlation analysis and ran into the following problem. I have a n by n matrix of p values and a corresponding n by n matrix of r. I want to keep every entry in the r matrix that has a p value less than 0.01, and make all the other entry either 0 or NaN. In other words, I have matrix A and B; if Ai,j then keep Bi,j; if Ai,j=0.01, then Bi,j=0. Can someone please help me with how to code this? Thank you!!

 Accepted Answer

If I understand your question correctly, then
x = A < 0.01; % Logical indexing for those elements you want to keep
B(~x) = nan; % Set the others to nan (or 0 if you want)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!