Why are my vectors different lengths?

Hi,
I'm trying to create timeseries plots from zones on a map. So I have done this:
II = 360; %Number of longitude points
JJ = 180; %Number of latitude points
%The time span is 09/97 to 09/14 (inclusive)
NN = 205; %Number of months
%==========================================================
% Defining longitude and latitude vectors
%==========================================================
for i=1:II
lon(i)=1.0*(i-1)-179.5;
end
for j=1:JJ
lat(j)=1.0*(j-1)-89.5;
end
%=========
I then read in the data followed by:
i1=106;i2=119;j1=167;j2=172;
for n=1:NN
sm=0.0;
cnt=0.0;
for i=i1:i2
for j=j1:j2
if data(i,j,n)>0
sm=sm+data(i,j,n);
cnt= cnt+1;
if cnt>0
ts(n)=sm/cnt;
else ts(n)=NaN;
end
end
end
end
end
i1=111;i2=125;j1=163;j2=166;
for n=1:NN
sm=0.0;
cnt=0.0;
for i=i1:i2
for j=j1:j2
if data(i,j,n)>0
sm=sm+data(i,j,n);
cnt= cnt+1;
if cnt>0
ts2(n)=sm/cnt;
else ts2(n)=NaN;
end
end
end
end
end
When I plot ts it works fine, but when I try and plot ts2 I get the error
"Error using plot Vectors must be the same length.
Error in TimesSeriesIce (line 180) pl = plot(dates, ts2, 'g') ; % plotting the x,y timeseries"
In the workspace it says ts is 1X205 and ts2 is 1X203. Not sure if I'm just being silly but I can't work out why they'd be different lengths. Does anyone know why this is ??
Thank you so much for any advice :)

1 Comment

Chad Greene
Chad Greene on 10 Apr 2015
Edited: Chad Greene on 10 Apr 2015
Hi Cara, welcome to the forum.
Some notes not exactly related to your question:
1. It's good practice to avoid using i or j as variables, because they're already built into Matlab as the imaginary unit. Doesn't often cause problems, but when it does, it can be a real headache.
2. That's an awful lot of nested loops. Loops aree computationally slow, and nesting loops makes computation very slow. But worse than having slow code, nested loops introduce lots of room for error and they're tough to decipher. It's hard to visually make sense of nested loops, which makes it tough to track down the source of errors. One thing I highly recommend: whenever you're tempted to use a loop for anything, post a question on the forum asking how you can avoid using the loop. Sometimes loops are okay. Sometimes loops are even the best solution. But very often loops are bad, and it's good to learn when to use them.
By inspection, it's hard for me to understand what sm, ts, and ts2 are supposed to be. Can you describe in words what these variable should represent?

Sign in to comment.

Answers (1)

Torsten
Torsten on 10 Apr 2015
There seem to be two indices n (1<=n<=NN) such that data(i,j,n) <= 0 for all 111<=i<=125, 163<=j<=166 ...
Best wishes
Torsten.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 10 Apr 2015

Edited:

on 10 Apr 2015

Community Treasure Hunt

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

Start Hunting!