I want my NaN values of one matrix to transfer over to a second matrix. Help!

How would I go about doing this? For example I have two matrices of equivalent size:
[1 5 NaN 4 NaN 10 3]
[5 2 8 2 5 2 6]
I want my NaN values of the first matrix to replace the numbers in the second matrix, in the order in which they were located in the first matrix. My final result would look like:
[1 5 NaN 4 NaN 10 3]
[5 2 NaN 2 NaN 2 6],
where 8 and 5 of the second matrix were replaced by NaN.
Help please!

 Accepted Answer

Try this:
a = [1 5 NaN 4 NaN 10 3]
b=[5 2 8 2 5 2 6]
b(isnan(a)) = nan

1 Comment

Perfect. Would you happen to know how to do this same process but for two different sized matrices, where I want to insert NaN in particular spots, with the number I'm "replacing" being shifted "down"? For example:
a = [1 5 NaN 4 NaN 10 3]
b = [6 1 0 3 5]
I want b to look like:
b = [6 1 NaN 0 NaN 3 5]
So essentially my NaNs are located in the same index as matrix 1, however, I want to preserve the numbers (not replace them like the first question) in b, by moving them down by one index in my new matrix.
Any tips?

Sign in to comment.

More Answers (0)

Asked:

on 16 Jul 2014

Edited:

on 16 Jul 2014

Community Treasure Hunt

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

Start Hunting!