tags:

views:

31

answers:

1

The code below gives me error = Subscript indices must either be real positive integers or logicals.Because the first index using FIND function is i=1, the i-1 gives negative value. How to ignore when i==1 without using the LOOP function

fid=fopen('data.txt');
A=textscan(fid,'%f%f%f%f');
fclose(fid);
in = cell2mat(A);

i = find(in(1:end,2)>20 & in(1:end,2) <50);
out=in;
s(i)=sqrt((out(i,3)-out(i-1,3))*(out(i,3)-out(i-1,3))+(out(i,4)-out(i-1,4))*(out(i,4)-out(i-1,4)));

fid = fopen('newData.txt','wt');  
format short g;
fprintf(fid,'%g\t%g\t%g\t%g\n',out',s');  %'# Write the data to the file
fclose(fid);
+2  A: 

you can try something like this:

i = find(in(1:end,2)>20 & in(1:end,2) <50);

i = i(find( i > 1));

By the way, be careful of using i as a regular variable because you're overriding the default value of:

i = sqrt(-1)

Nathan Fellman
Thanks ...but it gives me this error... Inner matrix dimensions must agree
Jessy
You probably want ".*" for elementwise array multiplication rather than "*" for matrix-matrix multiplication
Edric
Thanks Edric ..
Jessy
@Nathan Fellman: As long as you write the imaginary number as `1i`, i.e. the same way it's returned by Matlab, you can use i in e.g. iterations as much as you want, and there won't be any problem.
Jonas

related questions