Subscript indices must either be real positive integers or logicals.

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

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

I ran that and now I'm getting ''Subscripted assignment dimension mismatch.''
I assumed from your code that ‘f1T’ and ‘f2T’ are vectors. If they are matrices, you have to make the appropriate changes in your code. (Details are important!)
Try something like this for a matrix ‘f1T’:
for i=1:size(f1T,1)
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
That code works with an example matrix and scalar value for ‘s1’. Change your other loop similarly.
You will have to experiment with your data to get the result you want. I have no idea what your data are, or what you are calculating.
f1T and f2T are vectors yes, while s1 and s2 are matices. When I changed f1T and f2T to matrices also I got 'Subscript indices must either be real positive integers or logicals' again.
f1T and f2T are 1x15 and 1x49 column vectors while s1 and s2 are 15x13189 and 49x13189 matrices. They're split up because the step size in f2T is different to that in f1T
It would have helped to have had that information in the beginning.
This works with my test matrices and vectors:
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
It concatenates the individual matrix calculations, creating 3D matrices. You can then use the sum function to sum them across the appropriate dimensions.
That has solved the problem with the for loops thanks. However it outputs a result for m0 and mneg as one single value whereas I'm looking for the output to be a 1x13139 matrix.
Is it possible to break the 3D matrices down to 2D matrices somehow? Currently m0f1/m0f2 and mnegf1/mnegf2 are outputting 15x13139x15 and 49x13139x49 matrices when I require them to be 15x13139 and 49x13139 matrices.
Thanks for all your previous help with my code also.
My pleasure.
With respect to ‘m0’ and ‘mneg’, you have to decide how you want to calculate them. I have no idea what you are doing. Summing them twice over the first and third dimensions would produce the result of the size you want. I have no idea if it is the actual result you want:
Result_m0 = sum(sum(m0,1),3);
As for ‘m0f1’, ‘m0f2’ and the others, again I do not know what you are doing. You can get them as (15x13139) and (49x13139) matrices by summing them over the third dimension, if that is what you want to do:
Result_m0f1 = sum(m0f1,3);
and so for the others.
If my Answer helped you solve your problem, please Accept it.
Basically what I want to do is get an m0 and mneg value for each of the s values. I then want to sum each column of the m0 and mneg values to get my desired value i.e going from the 15x13139 and 49x13139 matrices to a 1x13139 matrix.
The way the data is laid out in the excel file is,
f(1) S1(1) S2(1) S3(1)… S13189(1)
f(2) S1(2) S2(2) S3(2)… S13189(2)
f(3) S1(3) S2(3) S3(3)… S13189(3)
f(64) S1(64) S2(64) S3(64)… S13189(64)
f is the frequency and s is an energy density. I want to multiply f(1) with all the other corresponding s(1) values [s1(1)-s13139(1)] and f2 with its corresponding values and so on using the m0f1/f2 and mnegf1/f2 formulas. Once I have these four values/matrices I pretty much have what I need.
It seems you’re doing that multiplication in the loops.
To sum over the columns (summing each row), use ‘sum(x,2)’, where ‘x’ is the array you want to sum over.
I admit that I’m lost. You will have to experiment with sum to get the result you want.
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?
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)

Categories

Community Treasure Hunt

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

Start Hunting!