How do I using logical values to write 'NAN' to non-existing data?

5 views (last 30 days)
I have a data set with non equal lengths
A = B =
H m lat H m lat
0 2 -90 0 1 -90
0 2 -89 0 1 -89
0 2 -88 0 1 -87
0 2 -87 0 1 -86
0 2 -86
I want to query using the lat of A to find missing lat in B and write 'NAN' in that row
so that the sizes become the same like this; I want to use this idea for a large data set
B=
0 2 -90
0 2 -89
NAN NAN NAN
0 2 -87
0 2 -86

Accepted Answer

Rik
Rik on 8 Nov 2022
You can use ismember:
A=[ones(5,2).*[0 2] (-90:-86).'];
B=[ones(4,2).*[0 2] [-90;-89;-87;-86]];
new_B=nan(size(A));
[~,rows]=ismember(B(:,3),A(:,3));
new_B(rows,:)=B;
new_B
new_B = 5×3
0 2 -90 0 2 -89 NaN NaN NaN 0 2 -87 0 2 -86
  4 Comments
Rik
Rik on 8 Nov 2022
You have posted code, but you didn't explain how the output is different from what you want.
Note that this code will overwrite any values that occur multiple times in later elements of A.
Stephen Tete
Stephen Tete on 8 Nov 2022
Edited: Stephen Tete on 8 Nov 2022
A is a cell like this
A = [180x4 double; 179x4 double; 180x4 double; 180x4 double; 178x4 double; 177x4 double; ...]
class(A)
>> 'cell'
each cell contains dataset with first element in A having full data set (size 180x4). some of the proceeding data do not have the full size (ex. 178x4 and 177x4). In this case i want to use column 3 of the first cell data to query those that do not have the full data(all data has the same elements in column 3) and put NAN at rows where there is no data.but i do not want to overwrite the outputs.
When i use your initial code to do for only two cells it works nicely but now i want to loop through all the cells in A and do for each of them once and save the output cells having all 180x4 after inserted inserting NANs
That was why i used this code
refom = nan(size(A{1}));
for i = 2 : length(A)
[~,rows] = ismember(A{i}(:,3),A{1}(:,3));
refom(rows,:) = A{i};
end
the output i desire should look like this
refom =
180x4 double
180x4 double
180x4 double
180x4 double
180x4 double
180x4 double
% so that if i open each cell i see data having both int and nans
but i get only the last cell which means the code overwrites the preceeding outputs to display the final last one
i = 200
refom = 180x4
-------------------------------------------------------------------------------------------
you can find few example of data in each cells here with heads
% H M lat TC
0 2 -90 0.701661800000000
0 2 -89 1.38124329032258
0 2 -88 1.50675797468354
0 2 -87 0.934005460000000
0 2 -86 1.30263100000000
0 2 -85 1.42799615000000
0 2 -84 1.94040264556962
0 2 -83 1.67793006666667
I hope this is elaborate

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!