Fixing MATLAB Error: P(1,0) Accessing Issue

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Error Matlab
Click For Summary

Homework Help Overview

The discussion revolves around a MATLAB coding issue related to accessing elements of a matrix. The original poster is encountering an error when trying to access an element of the matrix P using an index of 0, which is not allowed in MATLAB as it requires positive integer indices.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants explore the definition and indexing of the matrix P, questioning how to correctly access its elements without encountering indexing errors. There is a discussion about the implications of using 0 as an index and how to redefine the matrix or function to avoid this issue.

Discussion Status

Participants are actively discussing the nature of the matrix and its indexing. Some guidance has been offered regarding the need to use positive integers for indexing in MATLAB. The conversation indicates a productive exploration of the problem, with suggestions on how to redefine the indexing approach.

Contextual Notes

The original poster has defined the matrix P with dimensions based on a variable N, and there is a specific mention of the values of t(i) being integers from 1 to N+1. The discussion also highlights the need for clarity on how to evaluate the matrix P given the constraints of MATLAB's indexing system.

sara_87
Messages
748
Reaction score
0

Homework Statement



i'm trying to implement a code into MATLAB but I am getting an error that i think i understand but don't know how to fix it:

for i=1:n
f(x)=t(i)-*P(t(i),t0)*f(t0)
end

t0 is defined to be 0.
this is not the whole code, but the error lies in that line, it says:

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

Homework Equations





The Attempt at a Solution



I understand that the matrix P has an error because it's trying to evaluate at 0 when it should be a positive integer...? but this is the formula i want to evaluate.
 
Physics news on Phys.org
Is your matrix defined so that the columns are indexed from 1 through some number? If so, that's a problem if you try to access column 0.
 
what do you mean?
how do i define the matrix such that it starts from 1?
I only said:
P=zeros(N+1,N+1)
so that i initialize the matrix to zero.

?
 
sara_87 said:
? Attempted to access P(1,0); index must be a positive integer or logical.
Based on your error message, the problem seems to be the 0 index. If so, this suggests to me that the statement P=zeros(N+1,N+1) allocates a zero-filled N + 1 by N + 1 matrix. The entry at the upper left corner would be P(1, 1), and the entry at the lower right corner would be P(N+1, N+1). The first row has an index of 1, and the first column has an index of 1, so attempting to access P(0, *) or P(*, 0) would generate an error.
 
oh right, i see what you mean..there's no P(0,*) element.

but my function P is:
P=t^2+3T
so for f(x)=t(i)-*P(t(i),t0)*f(t0) i need to evaluate in the matrix P when t is t(i) and T is t0 and t0=0, so how can i do this in matlab?
 
From what you have said, P is a matrix, not a function. Your function appears to be f(x), and the value of f(x) is obtained from a specific cell in your matrix.

The matrix itself can have whatever values you choose to put in your matrix, but my quick reading of two MATLAB tutorials lead be to believe that MATLAB array indexes are 1-based, not 0-based. That means that when you access a matrix, both indexes have to be integers that are 1 or larger. So in particular, if you access your matrix like so -- P(t(i), t0) -- you are attempting to evaluate the matrix entry in the t(i)-th row and t0-th column. If either t(i) or t0 is 0, you'll get the error you showed.

It seems to me that you're going to need to rethink what you're storing in your matrix. With some more details about what you're doing, I might be able to help. My knowledge of MATLAB is extremely limited, but I am very proficient in a number of programming languages.
 
so if i have:
f(t(i))=t(i)-P(t(i),t0)*f(t0)
where t0=0, so the first few terms are:
f(t(1))=t(1)-P(t(1),0)f(t0)
f(t(2))=t(2)-P(t(2),0)f(t0)
etc...
but how can i put this into matlab?
 
How about this?
f(t(1))=t(1)-P(t(1),1)*f(t0)
f(t(2))=t(2)-P(t(2),1)*f(t0)
etc...
I.e., use column 1 instead of column 0 (=t0). It would also be helpful to know what the values of t(i) are.
 
oh right i see, so P(t(1),1) means that MATLAB will evaluate the matrix when t=
so if my matrix P is:

P=t(i)^2+3T,
this will evaluate: P(t(i),1st element)=t(i)^2
?
 
  • #10
I think you might be confusing the ideas of function and matrix. Unlike a function, a matrix doesn't do any computations. It is merely a container that can hold numbers (in your case). To store things in the matrix or to retrieve them, you need to provide the indexes.

Suppose I have this matrix, where P = [1.1 2.0 0; 0 4.5 -1.6; 10.3 -5.1 2.3]

[tex]P~=~\left[<br /> \begin{array}{ccc}<br /> 1.1&2.0&0.0\\<br /> 0.0&4.5&-1.6\\<br /> 10.3&-5.1&2.3\\<br /> \end{array}<br /> \right][/tex]

This is a 3X3 matrix. I can access the entry in the upper left corner using P(1, 1). The first index indicates the row (from 1 to 3); the second index indicates the column (from 1 to 3). As an example, P(3, 2) = -5.1.

It's not clear to me what your indexes are, since I don't know what t(1), t(2), etc. represent. For your problem, these have to be integers from 1 through N + 1, where presumably N was specified earlier.
 
  • #11
yes, N was specified, and t(i) are integers from 1 to N+1.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K