How can I select the aerosol values from different size of matrix?

1 view (last 30 days)
here, I have 3 different size of matrix:
First case; 2X2 matrix which look like [0,0.550;0,0.1293], second case is 1X2 matrix - [0.550,0], and third case is 4X2 matrix - [0.4430,1.1936;0.5550,1;0.670,0.8422;0.8650,0.6459]
Here I want select aerosol which are bolded in above matrix and i have written code but its not working. I am trying to plot my TOA variable with Aerosol but i have different size of matrix and having problem to select specfic aerosol value? So, can you guys give some ideas?
A = size([0 0; 1 1]);%just assigning 2X2 matrix for comparison
for dates = 1:115
for band = 1%[1 2 3 4 5 7]
SM_p029r029(dates).TOA = mean(SM_BQA(dates).SREF(band).TOA_Ref);
if size(Out(dates).aerosol)>=size(A)
SM_p029r029(dates).Aerosol1 = Out(dates).aerosol(2,2);
% figure(band)
% scatter(Aerosol1,SM_TOA,50,'MarkerEdgeColor','r','MarkerFaceColor','r','LineWidth',2);
% hold on
else
SM_p029r029(dates).Aerosol1 = Out(dates).aerosol(1,2);
% scatter(Aerosol1,SM_TOA,50,'MarkerEdgeColor','r','MarkerFaceColor','r','LineWidth',2);
end
end
end
save('Aerosol_TOA','SM_p029r029', '-v7.3');

Accepted Answer

Voss
Voss on 29 May 2022
Edited: Voss on 29 May 2022
Note that A is [2 2]
A = size([0 0; 1 1])
A = 1×2
2 2
so size(A) is [1 2]
size(A)
ans = 1×2
1 2
So when you compare size(Out(dates).aerosol) with size(A) here:
if size(Out(dates).aerosol)>=size(A)
you are checking size(Out(dates).aerosol) >= [1 2], and since size(Out(dates).aerosol) can be [1 2], [2 2] or [4 2], that comparison is always [true true]
size([0 0]) >= size(A)
ans = 1×2 logical array
1 1
size([0 0; 0 0]) >= size(A)
ans = 1×2 logical array
1 1
size([0 0; 0 0; 0 0; 0 0]) >= size(A)
ans = 1×2 logical array
1 1
so the code inside the if block is always executed, and element (2,2) of Out(dates).aerosol is always (attempted to be) retrieved
SM_p029r029(dates).Aerosol1 = Out(dates).aerosol(2,2)
but in case size(Out(dates).aerosol) is [1 2], that's an error because element (2,2) doesn't exist.
What you really mean to be doing is, instead of comparing to size(A), compare to A, since A is [2 2]
size([0 0]) >= A
ans = 1×2 logical array
0 1
size([0 0; 0 0]) >= A
ans = 1×2 logical array
1 1
size([0 0; 0 0; 0 0; 0 0]) >= A
ans = 1×2 logical array
1 1
That way, the else block will be executed when size(Out(dates).aerosol) is [1 2]. (A vector if condition is considered true only if all elements of the condition are true.)
However, you can do away with the if/else entirely and just do:
SM_p029r029(dates).Aerosol1 = Out(dates).aerosol(min(size(Out(dates).aerosol,1),2),2)
because the expression min(size(Out(dates).aerosol,1),2) gives you either the index of the last row of Out(dates).aerosol or 2, whichever is smaller, which is exactly what you need
min(size([0 0],1),2)
ans = 1
min(size([0 0; 0 0],1),2)
ans = 2
min(size([0 0; 0 0; 0 0; 0 0],1),2)
ans = 2
Or more succinctly:
SM_p029r029(dates).Aerosol1 = Out(dates).aerosol(min(end,2),2);

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!