Solving MATLAB Error: "Attempted to access p(1,0)

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Form Matlab Type
AI Thread Summary
The discussion revolves around resolving a MATLAB error related to accessing an invalid index in an array. The error occurs because the inner loop attempts to access an element where the index is zero, specifically when initializing the array `t`. The solution involves ensuring that the array `t` is properly initialized with values before being used in calculations. Additionally, participants clarify the distinction between using a function and a matrix for the variable `p`, emphasizing the need for clear definitions to avoid confusion. The final consensus is to ensure correct initialization and proper indexing to eliminate errors and achieve the desired matrix output.
sara_87
Messages
748
Reaction score
0

Homework Statement



I want to type in MATLAB form, the following:

a=3\sum^{i-1}_{j=1}p(t_i,t_j)y(t_j)

Homework Equations




The Attempt at a Solution



for i=1:n
t(i+1)=3*i
for j=1:i-1
a=3*p(t(i),t(j))*y(t(j))
end
end


Im getting the error:
? Attempted to access p(1,0); index must be a positive integer or logical.

how can i fix this?
 
Physics news on Phys.org


This is very similar to the other problem you posted in the Calculus and Beyond section, and the error you're getting here is exactly the same. The reason you're getting this error is that t(j) = 0 for some value of j.

Your inner loop runs from j = 1 to j = i -1. When i = 1 in the outer loop, j runs from 1 to 0, which is probably not the behavior you want. One way around this might be to treat i = 1 as a special case, and do what needs to be done, then have your outer loop run from i = 2 to n.
 


im understanding the problem, but can't fix it somehow.
i changed i to:
for i=2:n
t(i+1)=3*i
for j=1:i-1
a=3*p(t(i),t(j))*y(t(j))
end
end

but still get the error:

? Attempted to access p(1,0); index must be a positive integer or logical.
 


The problem has to be that for some value of j, t(j) = 0, and you're trying to access p(<something>, t(j)).

Let's hand-trace what's going on.
i = 2
t(3) = 3*2 = 6
j = 1
a = 3*p(t(2), t(1)) * y(t(1))
<inner loop ends>



It looks to me like t(1) never got properly initialized, so is probably zero. Since you outer loop initializes only t(3), t(4), ..., t(n + 1), you'll need to explicitly assign values to t(1) and t(2).
 


oh right, that makes sense. i initialized them to 0 and the error is now gone :)

I have:

p=t^2+3t
p(t(i+1),t(i+1))=('p',t(i))

and when i execute this, i get:
a matrix with 0 everywhere except at the diagonal, matrix a is:
0,0,0,0,0
0,4,0,0,0
0,0,10,0,0
0,0,0,18,0
0,0,0,0,0

so the diagonal terms are correct.. but i don't want this matrix, i want:

4,7,10,...
7,10,18,...

etc. so basically i want [t(i)]^2+3[t(j)] where here i is the row number and j is the column number.
how can i fix this?

thank you by the way.
 


This should do the trick for you. You need to put the values you need in the t() array.
Code:
% Code to initialize the t(i) array not shown

N = 5  % or whatever
for i = 1:N
   for j = 1:N
      P(i, j) = t(i)^2 + 3*t(j)
   end
end

Some things you said in your last post I don't understand.
sara_87 said:
i initialized them to 0 and the error is now gone
If you initialize t(1) and t(2) to zero, I don't see how that fixes the problem. When you try to access p(<something>, t(1)), you're still trying to access something in the zero column, which was the problem all along.

sara_87 said:
p=t^2+3t
p(t(i+1),t(i+1))=('p',t(i))
In the first line above, I don't know enough about MATLAB to know if you can assign the value of some expression (t^2 + 3t) to a matrix.
In the second line, what does 'p' mean? I would think it's just a single character, 'p', completely unrelated to your matrix.
 


because when i initialised only t(1) to zero and did: p(something, 1) as you advised from before...and this got rid of the error.

i think you're right about assigning some expression to a matrix...thats my problem now: i want to define: p=t(i)^2+3t(i).
and i also need: p=t(i)^2+3t(j) (which is a matrix).

'p' means it is using the function p (defined elsewhere) to evaluate p(t(i+1),t(i+1)).
 


i did:
Code:
% Code to initialize the t(i) array not shown

N = 5 % or whatever
for i = 1:N
for j = 1:N
P(i, j) = t(i)^2 + 3*t(j)
end
end

and it gave an error:
? Attempted to access t(2); index out of bounds because numel(t)=1.

i have no idea what this means.
 
sara_87 said:
because when i initialised only t(1) to zero and did: p(something, 1) as you advised from before...and this got rid of the error.
So initializing t(1) had nothing to do with getting rid of the error. Instead of p(something, t(1)), you changed to p(something, 1). That's what got rid of the error.
sara_87 said:
i think you're right about assigning some expression to a matrix...thats my problem now: i want to define: p=t(i)^2+3t(i).
and i also need: p=t(i)^2+3t(j) (which is a matrix).
Why do you want to do both of these things? Your matrix can hold only one set of values at a time. The code I provided should do the second thing, above. Decide which it is that you want to do, and I can help you with the code.
sara_87 said:
'p' means it is using the function p (defined elsewhere) to evaluate p(t(i+1),t(i+1)).
OK, now I'm confused. Is p a function or a matrix? I mentioned in the other thread that I thought you might be confusing these two very different concepts. If you actually have a function that evaluates your matrix, you should at the very least give it a name that's different from your matrix.

I mentioned before, that I know very little about matlab, but a lot about programming. If you don't have some reference manual to look at, here's a link to one I've been using: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf. Section 2-16 ff talks about arrays, as do sections 4-9 and 4-11. Sections 4-20, 4-21, and 4-22 would be a good start on understanding functions.
 
  • #10


sorry about many confusions, i think I am confused too. but...basically, this is what i need:


a(t(i))=b+c
where:
b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)
and
c=p(t(i),t(i))

p(t(i),t(i))=t(i)^2+3t(i).
p(t(i),t(j))=t(i)^2+3t(j).

that's why i need both of them.
am i making a mistake?
 
  • #11


sara_87 said:
sorry about many confusions, i think I am confused too.
:smile:
sara_87 said:
but...basically, this is what i need:


a(t(i))=b+c
where:
b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)
and
c=p(t(i),t(i))
So b and c are just scalars, right? I.e., they aren't lists of numbers or matrices with numbers in them. And a is just a one-dimensional array, right?
sara_87 said:
p(t(i),t(i))=t(i)^2+3t(i).
p(t(i),t(j))=t(i)^2+3t(j).
Your first formula is redundant. At the diagonal elements of your matrix, t(i) = t(j), so the second formula automatically gives you what the first formula does at any matrix element p(i, j) where the row number equals the column number. Or am I completely missing what you're trying to say.

The code I gave earlier accomplishes what I think you want.
sara_87 said:
that's why i need both of them.
am i making a mistake?
 
  • #12


yes, you're understanding my problem.
but,

for i=1:n
for j=1:i-1
p=t(i)^2 + 3*t(j)
b=p(t(i),t(j))*a(t(j))
end
end

gives b as a vector of all zeros.
 
  • #13


sara_87 said:
yes, you're understanding my problem.
but,

for i=1:n
for j=1:i-1
p=t(i)^2 + 3*t(j)
b=p(t(i),t(j))*a(t(j))
end
end

gives b as a vector of all zeros.
First, I'm not sure what this line means: p=t(i)^2 + 3*t(j)
I think what you want is this: p(i, j) = t(i)^2 + 3*t(j)
Second, the line above assumes that you have known values in your array t. IOW, you need to put some values in the array t before you use it to calculate values to be stored in another array.
Third, b should be an array (dimension 1), otherwise the value stored in a previous iteration of the loop will be overwritten. More importantly, the array a needs to have values stored in it before you use it to compute other values.
Fourth, you are storing something -- 0 apparently -- in b somewhere around n^2 times, which is way too many times. I don't believe that line should be inside your inner loop, but maybe should be in the outer loop after the first end statement.

Can you show me your complete script? I need to know how every variable is being initialized.
 
Back
Top