Index exceeds matrix dimensions. I can't figure out what I'm doing wrong, please help.

here is my code:
if true
S=[];
n=2;
w=abs(10^6);
v=1:998;
u= -1:1;
for a = -2 : 0.04 : 2
for b = -2 : 0.04 : 2
vec=[0;0];
n= 3: 1000; %n=1 and n=2 both equal 0
v(n+1)= a*v(n) + b*v(n-1) + u(n);
c= a + 1i*b; %using i instead of j
while (v(n+1) < w)
u= 1-2*rand(1);
v=vec;
n=n+1;
end %end while loop
if v(n+1) < w
S= [S c];
else
S=0;
end %end if statement
end %end b= for loop
end %end a= for loop
T=plot(S, '*');
axis(T,'square')
end
I don't know what this is supposed to plot cause i keep getting an "Index exceeds matrix dimensions." error. My TA didn't explain squat about what needed to be done and I really don't understand any sort or programming(even though I try my best it just doesn't click in my brain). An explanation would also be appreciated if anyone can help me. Thanks a bunch.

Answers (1)

We can’t figure out what you’re doing wrong either, because you haven’t told us what line is throwing the error, the size of the array you are indexing into, or the value of the index in the line that is throwing the error.
We’re very good at MATLAB, but we’ve proven over time to be absolutely hopeless mind-readers.

12 Comments

i don't know the line or value the error is being thrown at. my teacher never specified the array size xD here is my TA's intro to what were supposed to do: "A linear filter acts on a sequence u[n] of input samples to produce an sequence v[n] of output samples, where n is an integer. At time n+1, the output (sample) is given by
v[n+1] = a*v[n] + b*v[n-1] + u[n] (*)
In other words, the current output is a weighted average of the current input and the the previous two outputs. ALL SAMPLES AND COEFFICIENTS ARE REAL-VALUED.
This filter is stable if for EVERY input sequence u[n] that takes all its values in the interval [-1,1], the resulting output sequence v[n] is bounded, i.e., there exists a finite value M such that v[n] is in the interval [-M,M].
For each value of (a,b) on the discrete grid
a = -2 : 0.04 : 2 ;
b = -2 : 0.04 : 2 ;
the filter will be tested for stability by computing the output sequence corresponding to a random input sequence generated by the MATLAB function RAND. The filter is deemed to be stable if by time n=1000, all output samples are less than 10^6 in absolute value. Those pairs (a,b) that pass the test are saved as a+j*b in a vector S, which is then plotted."
MATLAB will tell you what line is throwing the error.
Please copy the entire red error message, including the line that is throwing the error. MATLAB should produce all this information, but it will at the very least provide you with the line number. That will allow you to find the line and copy it to your Question or Comment.
Please also provide the other information, such as the size of the array you are indexing, and the value of the index when your code throws the error.
if true
EDU>> S=[];
n=2;
w=abs(10^6);
v=1:998;
u= -1:1;
for a = -2 : 0.04 : 2
for b = -2 : 0.04 : 2
vec=[0;0];
n= 3: 1000; %n=1 and n=2 both equal 0
v(n+1)= a*v(n) + b*v(n-1) + u(n);
c= a + 1i*b; %using i instead of j
while (v(n+1) < w)
u= 1-2*rand(1);
v=vec;
n=n+1;
end %end while loop
if v(n+1) < w
S= [S c];
else
S=0;
end %end if statement
end %end b= for loop
end %end a= for loop
T=plot(S, '*');
axis(T,'square')
Index exceeds matrix dimensions.
end
Matlab isn't telling me what line is throwing the error though S= [S c];<----this line is the only line giving me a warning, it tells me that i should consider preallocating for speed and i tried that and it didn't help or i just did it incorrectly. xD
Before your for loop, add this line:
S = [];
If you know how large your ‘S’ array will be (my guess is (1x10201), and since it seems to be a vector, preallocate it as:
S = nan(1,10201);
again before the loop. Then subscript the individual elements as you add them to ‘S’, such as:
S(k1) = c;
You will have to figure out how to calculate ‘k1’. The easiest way would simply be to use a counter and not worry about calculating it from the other indices. The errors and warnings should then go away.
now its telling me that 'S' is changing size on every loop iteration. i understand what this is telling me. Did i put
S= [S c];
in the wrong spot? that's the line that's giving me the warning.
It’s changing size at every iteration because you decided to not preallocate ‘S’. Don’t worry. If your code runs and gives the results you expect, then the only problem is that it will be a bit slower and less efficient than if you had preallocated ‘S’.
I am still get the limit exceeds matrix dimension error. And thank you very much for your help.
My pleasure.
What line is throwing the error? We need to see the line itself, not simply the line number.
Index exceeds matrix dimensions.<---this is not telling me what line it is which is just irritating.
Line22: S= [S c];
that line is still the only thing that telling me anything. is there a command that will tell me what is wrong?
What is the size of ‘S’, and what is the value and size of of ‘c’ when the error occurs?
c is supposed to equal each increment of a so from -2 to 2 increasing by .04 plus each increment of b(which is the same thing), while b is being multiplied by j. I made 'S' a size of 1x10000
That doesn’t mean anything to me. To fit in ‘S’ as you’ve written your code (at least as I understand it), ‘c’ must be a scalar. It cannot be a vector.

Sign in to comment.

Categories

Asked:

on 12 Feb 2015

Commented:

on 13 Feb 2015

Community Treasure Hunt

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

Start Hunting!