MATLAB MATLAB error when storing values in a matrix

AI Thread Summary
The user is encountering an error in MATLAB when attempting to store values in a matrix during a loop. The issue arises specifically at certain values of the loop variable 'i', leading to an "index must be a positive integer or logical" error. The output vector 'Y' is consistently a 1x2 vector, but the indexing into matrix 'X' fails at specific points. A suggested solution is to convert the index to an integer using `int32`, which may resolve the issue. The discussion highlights the importance of ensuring that matrix indices are always positive integers in MATLAB.
Firepanda
Messages
425
Reaction score
0
Basically I have a function file set up called BSform.m, I'm confident this has nothing to do with my problem

My problem is this driver file for the function

X=zeros(101,2);
for i = 0:0.01:1
[C1,P1]=BSform(200,200,2,0,0.02,i);
Y=[C1,P1]
X(100*i+1,1)=Y(1,1)
X(100*i+1,2)=Y(1,2)
end

My output Y ALWAYS gives me a 1x2 vector (this is what I want, the function is working as intended)

I am trying to STORE this vector in my X matrix

Everything is FINE up until i get to i=0.14, and I've no idea why

The error I get is:

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

Error in ==> File2 at 5
X(100*i+1,1)=Y(1,1)It's really weird since if I manually input this Y value it manages to access it fine and store it in X. I thought about overriding it and moving on, but then it screwed up when it hit X(29,1) and again at X(30,1) then again a few steps more, really irregular pattern.

Any idea what's causing this? I've tried transposing it all, and the same problem hits at the same values for i.

Thanks
 
Physics news on Phys.org
Why don't you convert index to integer, e.g.

X(int32(100*i+1),2)=Y(1,2)
 
Back
Top