"Subscript indices must either be real positive integers or logicals." again! See codes

273 views (last 30 days)
I have created two functions in Matlab. One is supposed to produce a concentration profile for a given location and time, but the current code gives this error "Subscript indices must either be real positive integers or logicals.". The second function, called testing, recalls the first function to do the calculation and uses loops instead, but still the same error is appearing.
1. Concentration profile code
function [c,t] = Disp_Conc(x,y,z,t)
%This function calculates the concentration of a an agent at a particular time and location
% Data generated through Dispersion model (This is a 1x61 vector)
[x,cc_x,b_x,betac_x,zc_x,sig_x,t,xc_t,bx_t,betax_t] = Read_Predict('predict');
% Time averaged volume concentration: concentration contour parameters
%erf = error function
sr2 = sqrt(2.0);
xb = (x-xc_t(t)-bx_t(t))/(sr2*betax_t(t));
ya = (y+b_x(x))/(sr2*betac_x(x));
yb = (y-b_x(x))/(sr2*betac_x(x));
za = (z-zc_x(x))/(sr2*sig_x(x));
zb = (z+zc_x(x))/(sr2*sig_x(x));
c = (cc_x(x) * (erf(xa)-erf(xb)) * (erf(ya)-erf(yb)) * (exp(-za*za)+exp(-zb*zb)));
**Running this function, I get an error saying "Subscript indices must either be real positive integers or logicals." due to the fact that values of t and x are both fractional (both x and t are read from a 1x61 vector). To overcome this, I created another .m file that recalls the concentration function and uses loops as below:
2. Function "testing":
function [c, t] = testing( x,y,z,t )
%The values of x and t are already being read from a saved 1x61 vector. z and y can vary and must be specified by the user for a given location. The value of z should be set equal to zero for all cases and doesn't change.
for ts=1:length(t)
for xs=1:length(x)
for y=0:100; %(k index?)
**Running "testing" for a particular parameters say testing(5,0,0,8.79), I get the below errors:
"Subscript indices must either be real positive integers or logicals.
Error in Disp_Conc (line 12)
Error in testing (line 10)
I don't know what is wrong this time? The original problem was because of indexing and now I still get the same error with the new function.
Any help would be greatly appreciated. Thx.

Accepted Answer

Star Strider
Star Strider on 10 Jan 2015
In the line that throws the error:
what is the value of ‘t’?

Sign in to comment.

More Answers (3)

Stephen23 on 10 Jan 2015
Edited: Stephen23 on 10 Jan 2015
In MATLAB all array indices must be logical or positive numeric integers. This means that the following is permitted:
>> A = [123,456,789];
>> A(2) % 2 is an integer
ans = 456
But the following produces an error:
>> A(3.14159) % 3.14159 is not an integer
Subscript indices must either be real positive integers or logicals.
Note that negative integers and zero are not permitted indices.
You need to review your code and check all of the array indexing. In particular it seems that you are using some data values (e.g. t, x) as indices.

Sign in to comment.

Luca on 21 Aug 2017
Edited: Walter Roberson on 21 Aug 2017
clc; clear;
v_quadro = g*R*cos(teta.*2) + R*g(2^0.5-1)-4*R^3*(1-cos(teta))*cos(teta)/((2*R-(2^0.5)*R)^2);
Where is the error, please? Thanks.

common fernando
common fernando on 11 Jun 2020
hi guys I do the code for white noise of 3LDFT algorithm but give me error
Subscript indices must either be real positive integers or logicals.
Error in threeDFT (line 37)
x=v(((j-1):(j+ N0-2))*(dt));
  1. function [t_est,f_est]=threeDFT(v,fs,tmax,N0)
  2. % v : volt as function of time
  3. % fs : sampling frequency (Hz)
  4. % tmax : time of final estimation
  5. % N0 : number of samples in the window
  6. % to test: [t,f]=threeDFT(@(t)(220*sin(2*pi*50.1*t+pi/2)),50*512,1,512)
  7. % to test: [t,f]=threeDFT(@(t)(220*sin(2*pi*50.1*t+pi/2)+randn(size(t))*.1),50*512,1,512)
  8. fs=50*512; %sampling freq.
  9. dt =1/fs;
  10. N0=fs/50; %number of samples/cycle
  11. m=50; %no. of cycles
  12. t = dt*(0:m*N0); %data window
  13. fi=50; %Frequency test
  14. ww=wgn(201,1,-40);
  15. size(transpose(ww))
  16. t =dt*(0:200);
  17. y=sin(2*pi*fi*t + 0.3);
  18. v=sin(2*pi*fi*t + 0.3)+transpose(ww)
  19. tmax=1;
  20. n=N0-1:-1:0;
  21. f0=50;
  22. f=50.88;
  23. Hc=2/N0*cos(2*pi*n/N0+pi/N0);
  24. Hs=-2/N0*sin(2*pi*n/N0+pi/N0);
  25. t_est=[];
  26. f_est=[];
  27. j_max=tmax*fs;
  28. for j=1:j_max+1
  29. x=v(((j-1):(j+ N0-2))*(dt));
  30. c(j)=x*Hc';
  31. s(j)=x*Hs';
  32. if(j>N0)
  33. Ac(j-N0)=sqrt(sum(c(end-N0+1:end).^2)/N0);
  34. As(j-N0)=sqrt(sum(s(end-N0+1:end).^2)/N0);
  35. cc(j-N0)=c(end-N0+1:end)*Hc';
  36. ss(j-N0)=c(end-N0+1:end)*Hs';
  37. if(j>2*N0)
  38. Acc(j-2*N0)=sqrt(sum(cc(end-N0+1:end).^2)/N0);
  39. Ass(j-2*N0)=sqrt(sum(ss(end-N0+1:end).^2)/N0);
  40. ccc(j-2*N0)=cc(end-N0+1:end)*Hc';
  41. ccs(j-2*N0)=cc(end-N0+1:end)*Hs';
  42. ssc(j-2*N0)=ss(end-N0+1:end)*Hc';
  43. sss(j-2*N0)=ss(end-N0+1:end)*Hs';
  44. ff=f0*N0/pi*atan(tan(pi/N0)*((ccc(j-2*N0).^2+ccs(j-2*N0).^2)./(ssc(j-2*N0).^2+sss(j-2*N0).^2)).^.25);
  45. t_est=[t_est;(j-1)*dt];
  46. f_est=[f_est;ff];
  47. end
  48. end
  49. end
  50. t_est;
  51. f_est
  52. plot(t_est, f_est,'red')
  53. hold on
  54. RMSE = sqrt(mean((f_est-fi).^2))
  55. xlabel('time')
  56. ylabel('frequency')
  57. title('3LDFT WHITE NOISE ')
  58. plot (t,fi)
  59. hold off
Steven Lord
Steven Lord on 13 Jun 2020
There's no such thing as element 1.005 of an array in MATLAB. If as line 19 states you want v to be a function of t rather than a vector created using a vector of values t you should probably define it as an anonymous function.
v = @(t) sin(2*pi*fi*t + 0.3)+transpose(ww);
If you do this, v(1.005) will not be an attempt to index into a vector but will be an attempt to evaluate the anonymous function when t is 1.005.
As a simpler example that shows the technique:
x = 1:10;
y1 = @(x) x.^2;
y1(5.5) % works, returns (5.5)^2 = 30.25
y2 = x.^2;
y2(5.5) % will not work

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by