How to choose overlapping data from two matrices?

I am trying to use a bit of code that uses two pieces of data to generate another (input a and b, get c) and then plot c by loction. 'a' and 'b' come from data collected from an area but not from the same exact locations, so I have used griddata to generate two matrices with interpolated 'a' and 'b' values, and plan to calculate the corresponding 'c' values from these. This way, I should be able to plot 'c' by location using pcolor.
The problem that I am having i that the calculation uses a 'root' function, and thus will not accept NaNa (or presumeably '0'). Since my original data was not collected in a nice square, I have some entire columns of NaN in my interpolated data matrices. I also have some columns with data for 'a' but no data for 'b', see example below:
'a': [NaN NaN 3 5 NaN; NaN 2 4 1 NaN; NaN 3 NaN 3 4]
'b': [NaN 3 2 4 NaN; NaN 1 2 2 3; NaN NaN 3 1 NaN]
Is there a way to replace the 'NaN' in my matrices with something else? Or perhaps extract only the data when a value appears in the same place in both matrices 'a' and 'b'? I'm not sure the second option would work, as I then want to plot the results versus location (original x and y).
Thanks for your help! Jen

 Risposta accettata

Index the NaNs with isnan:
idxa = isnan(a);
idxb = isnan(b);
Then combine the indexes with an or and negate it to find where both matrices have NON nans or use the indexes singularly to replace the nans with 0 or smt else:
a(isnan(a)) = 0;

2 Commenti

I have attempted to use the a(isnan(a))=0; however, this has rounded the rest of my data for four decimal places, which is insufficient to tell them apart!
It's a matter of display.
Try format long in the command window.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by