Subscript indices must either be real positive integers or logicals.

2 views (last 30 days)
I got the error message after running my code (shown below). I realise that the issue is that the data I'm reading in is real numbers(decimals) and not integers. Is there any work around for matlab or way of rectifying this? The error is occuring in both the for loops. Thanks for any help provided.
f1T=transpose(f1);
f2T=transpose(f2);
i=f1T;
j=f2T;
for i=f1T
m0f1(i)= 0.5*((s1*(i+0.05))+(s1*i)*((i+0.05)-i));
mneg1f1(i)= 0.5*(s1*(i+0.05))+(s1*i)/(0.5*((i+0.05)+i))*((i+0.05)-i);
end
for j=f2T
m0f2(j)= 0.5*((s2*(j+0.1))+(s2*j)*((j+0.1)-j));
mneg1f2(j)= 0.5*(s2*(j+0.1))+(s2*j)/(0.5*((j+0.1)+j))*((j+0.1)-j);
end
m0=sum((m0f1(i))+(mof2(j)));
mneg1=sum((mneg1f1(i))+(mneg1f2(j)));
Hs=4*(SQRT(m0)); Te=mneg1/m0;

Accepted Answer

Star Strider
Star Strider on 6 Jul 2017
Subscripts must be integers greater than zero in MATLAB.
I would do something like this:
for i=1:length(f1T)
m0f1(i)= 0.5*((s1*(f1T(i)+0.05))+(s1*f1T(i))*((f1T(i)+0.05)-f1T(i)));
mneg1f1(i)= 0.5*(s1*(f1T(i)+0.05))+(s1*f1T(i))/(0.5*((f1T(i)+0.05)+f1T(i)))*((f1T(i)+0.05)-f1T(i));
end
for j=length(f2T)
m0f2(j)= 0.5*((s2*(f2T(j)+0.1))+(s2*f2T(j))*((f2T(j)+0.1)-f2T(j)));
mneg1f2(j)= 0.5*(s2*(f2T(j)+0.1))+(s2*f2T(j))/(0.5*((f2T(j)+0.1)+f2T(j)))*((f2T(j)+0.1)-f2T(j));
end
See if that works.
  10 Comments
James Prendergast
James Prendergast on 7 Jul 2017
OK. To be honest if I can just get the 15x13139 and 49x13139 matrices I'll be able to get my desired result from those. With regards to your solution for getting a 2d matrix from the 3d one, I only require the first two dimensions (the 15x13139 from the 15x13139x15 if that makes sense). Is that what your solution is doing or is it combining the third dimension into the first two?
Star Strider
Star Strider on 7 Jul 2017
I understand that you are importing an Excel file (two, actually), and want to multiply the first column by the others in each row for each one. That you can do with one bsxfun call.
Example
Rs = 5; % Row Size
Cs = 7; % Column Size
A = randi(9, Rs, Cs); % Create Data (Matrix Imported From Your Excel File)
Fv = A(:,1); % Frequencies Are First Column
Se = A(:,2:end); % ‘Signal Energy Density’ Are The Other Columns
M = bsxfun(@times, Fv, Se); % Multiply ‘Fv’ By All Other Elements In Same Row
This much I understand from your previous Comment. These should give you the 2D matrices you want. Do the same thing for each of them.
I still do not understand how you calculate ‘m0’ and ‘mneg’, and what the difference is between them.
It seems that you then want to vertically concatenate your two matrices, and then sum them column-wise, to get your (1x13139) matrix. That is easy enough to do with one sum call.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!