How can I do the average difference of two dataset?

for date = 1:436
for band = [1 2 3 4 5 7]
L2= L2_p181r040(date).SREF(band).Sur_Ref_L2_MASK;
SM = SM_p181r040(date).SREF(band).Sur_Ref_Mask_Normalized;
if isequal(size(L2),size(SM))
Diffs(date).AbsDiff(band).Ref_Diff = mean(SM-L2);
Band_DIFFs(band).diff(date) = Diffs(date).AbsDiff(band).Ref_Diff;
end
end
end
Hello all, I am getting error - Index exceeds the number of array elements (0).
For some of the dates, I don't have data for L2 variable but have all the dates for SM. I want to calculate the average difference between the two dataset (SM & L2), for only the dates which L2 have, so can you please help in this.

 Accepted Answer

for date = 1:numel(L2_p181r040)
% skip L2_p181r040(date) if L2_p181r040(date).SREF is empty
if isempty(L2_p181r040(date).SREF)
continue
end
% otherwise do the thing
for band = [1 2 3 4 5 7]
L2 = L2_p181r040(date).SREF(band).Sur_Ref_L2_MASK;
SM = SM_p181r040(date).SREF(band).Sur_Ref_Mask_Normalized;
if isequal(size(L2),size(SM))
Diffs(date).AbsDiff(band).Ref_Diff = mean(SM-L2);
Band_DIFFs(band).diff(date) = Diffs(date).AbsDiff(band).Ref_Diff;
end
end
end

6 Comments

Thank you for quick response.
I am trying to plot it one-to-one but its taking a lot of time. Is there a other way, that I can plot it 1-1 line.
for date = 1:numel(L2_p181r040)
% skip L2_p181r040(date) if L2_p181r040(date).SREF is empty
if isempty(L2_p181r040(date).SREF)
continue
end
% otherwise do the thing
for band = [1 2 3 4 5 7]
L2 = L2_p181r040(date).SREF(band).Sur_Ref_L2_MASK;
SM = SM_p181r040(date).SREF(band).Sur_Ref_Mask_Normalized;
if isequal(size(L2),size(SM))
scatter(L2,SM,15,'Marker','s','MarkerEdgeColor','r',);
end
hold on
plot([0 max([xlim ylim])], [0 max([xlim ylim])], '--r','LineWidth',2);
end
end
If the variables xlim and ylim don't ever change - which I can't say because they aren't defined - then you can plot that line once at the end, after both loops.
Another thing you can do is collect the relevant L2 and SM (i.e., the ones whos size is equal) in cell arrays, and scatter plot them after the loops.
I have defined the xlim and ylim, while coping code I forget to paste here.
How can I store L2 and SM in cell arrays?
I figured xlim and ylim were defined in your code, but since I can't see that, I don't know whether they change or are constant.
To store L2 and SM in cell arrays:
L2 = cell(numel(L2_p181r040),7);
SM = cell(numel(L2_p181r040),7);
for date = 1:numel(L2_p181r040)
% skip L2_p181r040(date) if L2_p181r040(date).SREF is empty
if isempty(L2_p181r040(date).SREF)
continue
end
% otherwise do the thing
for band = [1 2 3 4 5 7]
L2{date,band} = L2_p181r040(date).SREF(band).Sur_Ref_L2_MASK;
SM{date,band} = SM_p181r040(date).SREF(band).Sur_Ref_Mask_Normalized;
if isequal(size(L2{date,band}),size(SM{date,band}))
scatter(L2{date,band},SM{date,band},15,'Marker','s','MarkerEdgeColor','r',);
end
hold on
plot([0 max([xlim ylim])], [0 max([xlim ylim])], '--r','LineWidth',2);
end
end
Now it's works. Thank you so much.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 6 May 2022

Commented:

on 6 May 2022

Community Treasure Hunt

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

Start Hunting!