How to select common data in two different variables

Dear matlabers I have two variables with dimensions (lat,lon,time), var1 is for depth 300 and var2 for depth 700. I want to add these two variables but I do not have the data in the same location in both variables i mean some has Nan value.
So i want to add the variables where we have data in the same location in both variables other discarded.
any help will appreciated

4 Comments

Hi Abhishek
you need to define the degree of accuracy that you accept as a match to select, if you want 2 decimals precision, then 22.347 is going to be a match to 22.34X any on the other variable.
In any case, would it be possible for you to show sample data for both variables so readers get a clearer idea of what do you consider a match, for then select, and what has to be discarded?
regards
John BG
Dear John
Thanks for your quick reply. My simple aim is to add two variables where they have data in both variables at a location and give a NaN for all other situation (like var1 have data but var2 has NaN in the same location or var1 has Nan and var2 has data,these condition i want to exclude during addition of two var).
Many thanks
Can you give us a bit of code or some numbers as an example? Adding NaN's to finite values results in a NaN so you may just be able to pad the shorter vector to match the length of the longer vector (with NaN's) and then add them together.
Example: V1 = [1 2 NaN 4]; V2 = [1 NaN NaN 5 NaN NaN]
If you pad V1 with two NaN's at the end you can just get the result: V1+V2 = [2 NaN NaN 9 NaN NaN]
Is this along the lines of what you're looking for?
Thank you Chad for your advice and its work.

Sign in to comment.

Answers (0)

Asked:

on 30 Apr 2018

Commented:

on 1 May 2018

Community Treasure Hunt

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

Start Hunting!