Why does it say "Subscripted assignment dimension mismatch" ? possibly because of NaN's?

I am interpolating triscattered data onto a regular grid. It works but then stops at t = 10, I don't know why it is doing this!.. I think i had this problem a while back and I managed to sort it but I can't seem to fix it this time. Does anyone know what is wrong? see after code for data dimensions...
weight = cos(REF_lat);
[x,y]=meshgrid(0:1:360,-66:1:66);
x=x(1:end-1,1:end-1); y=y(1:end-1,1:end-1);
load('/work/uo0122/u253082/Mat_Time_Subsampled/STORM_10d_TJJ_ssh_orig_sampling.mat');
s = STORM_10d_TJJ_ssh_orig_sampling;
mask = zeros(size(depth));
mask(find(depth > 0)) = 1;
AM = ones(663,132,360);
Wrep = ones(663,132,360);
for t = 1:size(s,1);
t
tic
a=squeeze(s(t,:,:));
a=a(:);
bad1=find(isnan(a)==1);
a(bad1)=[];
lon=REF_lon(:);
lon(bad1)=[];
lat=REF_lat(:);
lat(bad1)=[];
w = weight(:);
w(bad1)=[];
F1=TriScatteredInterp(lon,lat,a,'natural');
W1=TriScatteredInterp(lon,lat,w,'natural');
a_m=F1(x,y);
AM(t,:,:)=a_m;
W=W1(x,y);
Wrep(t,:,:)=W;
toc
end
weight = 3127 x 254 s = 663 x 3127 x 254 * you can ignore mask
The data I get afterwards looks reasonable too. My biggest suspicion is that it is because it is a timestep that is just NaN's, I tried removing the NaN's but I couldn't seem to do it properly.
Thanks in advance! Michael

8 Comments

I think the problem is when you tried to remove NaN inside a for loop.
Michael - which line of code is throws the error? Since it is failing only at t equal to 10, then why not step through the code with the debugger?
Either put a breakpoint at the line that throws the error, run the code, and hit continue until you get to the tenth iteration. Or, prior to running the function (or script), run the following command in the Command Window
dbstop if error
Now as you run your function (or script), when an error is encountered the debugger will jump to the line in the function that threw the error.
You can now examine each variable on the left- and right-hand side of the assignment and determine why the subscripted assignment dimension mismatch error is thrown.
An example of this type of error is when an assignment of too little or too much data is being made. For example, suppose we do the following
% create a 4x4 matrix of ones
Z=ones(4,4);
% assign a 1x4 vector to a 1x2 space within the matrix
Z(2,1:2) = ones(1,4);
The above is impossible, and the
Subscripted assignment dimension mismatch.
error is thrown.
I think I need to remove the NaN's within a for loop so I'm not sure thatI can change that. This procedure worked for another data set and when t = 10, each dimension of s is = NaN's.
Geoff, It seems to be at this line:
a_m=F1(x,y);
F1, lon, lat, weight = [], so they are now empty..
If F1, lon, lat, and weight are all empty, would that have meant that all of a (or s(10,:,:)) would have been all NaNs? If so, can this still be considered valid data?
yes, but just for this one timestep. I currently have this:
for t = 10:size(s,1); t tic a=squeeze(s(t,:,:));
a=a(:);
bad1=find(isnan(a)==0);
a1 = a(bad1);
lon=REF_lon(:);
lon1 = lon(bad1);
lat=REF_lat(:);
lat1 = lat(bad1);
F1=TriScatteredInterp(lon1,lat1,a1,'natural');
a_m=F1(x,y);
AM(t,:,:)=a_m;
toc
end
but it still doesn't work for t = 10. I basically need to change the NaN's = 0's I think.
Why not just skip it
a(bad1) = [];
if isempty(a)
% maybe set all AM data to zero for this t
AM(t,:,:) = 0;
else
lon=REF_lon(:);
lon(bad1)=[];
lat=REF_lat(:);
lat(bad1)=[];
w = weight(:);
w(bad1)=[];
F1=TriScatteredInterp(lon,lat,a,'natural');
W1=TriScatteredInterp(lon,lat,w,'natural');
a_m=F1(x,y);
AM(t,:,:)=a_m;
W=W1(x,y);
Wrep(t,:,:)=W;
end
In the code of your last comment, you seem to be trying to use the NaN data even though it is invalid so am not sure how that would work.

Sign in to comment.

 Accepted Answer

For the case of t being equal to ten, it was found that the line of code that was generating the Subscripted assignment dimension mismatch error was
a_m=F1(x,y);
The variables F1, lon, lat, and weight were all empty due to the all elements in the s(t,:,:) matrix being set to NaN.
A solution was to address this in the code as follows - if it was found that the a matrix was empty (after the removal of NaNs) then do nothing (or set the output to be all zeros) and just continue with the next iteration of the for loop
a=squeeze(s(t,:,:));
a=a(:);
bad1=find(isnan(a)==1);
a(bad1) = [];
if isempty(a)
% maybe set all AM data to zero for this t
AM(t,:,:) = 0;
else
lon=REF_lon(:);
lon(bad1)=[];
lat=REF_lat(:);
lat(bad1)=[];
w = weight(:);
w(bad1)=[];
F1=TriScatteredInterp(lon,lat,a,'natural');
W1=TriScatteredInterp(lon,lat,w,'natural');
a_m=F1(x,y);
AM(t,:,:)=a_m;
W=W1(x,y);
Wrep(t,:,:)=W;
end

2 Comments

thanks, I ended up using this command in the world, but in a musch simpler way within the loop:
if isempty(a_m) == 0;
AM(t,:,:)=a_m;
end
Good that you have it working. Am just unsure of one thing: I thought that the error was being generated from the previous line
a_m=F1(x,y);

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Products

Asked:

on 4 Aug 2014

Edited:

on 6 Aug 2014

Community Treasure Hunt

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

Start Hunting!