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

In summary: In summation, p is a function, but t is a matrix. You can't assign an expression to a matrix, like you can to a function.
  • #1
sara_87
763
0

Homework Statement



I want to type in MATLAB form, the following:

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

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
  • #2


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.
 
  • #3


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.
 
  • #4


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).
 
  • #5


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.
 
  • #6


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.
 
  • #7


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)).
 
  • #8


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.
 
  • #9
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:
[tex]b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)[/tex]
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:
[tex]b=\sum^{i-1}_{j=1}p(t_i,t_j)a(t_j)[/tex]
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.
 

What does the MATLAB error "Attempted to access p(1,0)" mean?

This error means that you are trying to access an element in a matrix or vector that does not exist. In this case, you are trying to access the element at position (1,0) which is not a valid index.

How can I fix the "Attempted to access p(1,0)" error in MATLAB?

To fix this error, you need to make sure that the index you are trying to access is within the bounds of the matrix or vector. For example, if your matrix has 3 rows and 3 columns, the valid indices are (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3). Trying to access an element at position (1,0) would result in an error.

Why am I getting the "Attempted to access p(1,0)" error when my code used to work before?

It is possible that your code used to work before because the matrix or vector you were working with had different dimensions. If the dimensions have changed, the index you are trying to access may no longer be valid, resulting in this error.

Is there a way to prevent the "Attempted to access p(1,0)" error in MATLAB?

Yes, you can prevent this error by always checking the dimensions of your matrix or vector before accessing elements at specific indices. You can also use the built-in functions such as "size" to get the dimensions of your matrix or vector before accessing elements.

Are there any other common errors related to accessing elements in MATLAB?

Yes, some other common errors include "Index exceeds matrix dimensions", "Subscript indices must either be real positive integers or logicals", and "Array indices must be positive integers or logicals". These errors also occur when you are trying to access elements at invalid indices.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
  • Engineering and Comp Sci Homework Help
Replies
1
Views
882
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
732
  • Differential Equations
Replies
1
Views
770
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top