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

조회 수: 120 (최근 30일)
Hi,
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);
xa=(x-xc_t(t)+bx_t(t))/(sr2*betax_t(t));
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)));
time=tm(t);
end
**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)
z=0;
for y=0:100; %(k index?)
X=x(ts);
T=t(xs);
[c(ts),time(ts)]=Disp_Conc(X,y,z,T);
end
end
end
end
**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)
xa=(x-xc_t(t)+bx_t(t))/(sr2*betax_t(t));
Error in testing (line 10)
[c(ts),time(ts)]=Disp_Conc(X,y,z,T);"
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.

채택된 답변

Star Strider
Star Strider 2015년 1월 10일
In the line that throws the error:
xa=(x-xc_t(t)+bx_t(t))/(sr2*betax_t(t));
what is the value of ‘t’?
  댓글 수: 10
Summer
Summer 2015년 1월 10일
"xa(k1) = (x-xc_t(k1)+bx_t(k1))/(sr2*betax_t(k1));"
Worked this time! Thanks :)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Stephen23
Stephen23 2015년 1월 10일
편집: Stephen23 2015년 1월 10일
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.
  댓글 수: 4
Stephen23
Stephen23 2015년 1월 10일
True, [] simply returns an empty array. I have edited my answer to reflect this.

댓글을 달려면 로그인하십시오.


Luca
Luca 2017년 8월 21일
편집: Walter Roberson 2017년 8월 21일
clc; clear;
teta=linspace(0,pi/2,100);
R=1;
g=9.81;
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.
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 8월 21일
Your g is not a vector, but you are trying to index it at (2^0.5-1).
MATLAB does not have any implicit multiplications. You need to add ".*" or "*" in g(2^0.5-1)

댓글을 달려면 로그인하십시오.


common fernando
common fernando 2020년 6월 11일
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
  댓글 수: 3
common fernando
common fernando 2020년 6월 12일
I have another error
clear all; close all; clc
Attempted to access v(1.005); index must be a positive integer or logical.
Error in code (line 41)
v_new=v(i.*dt);
>>
  1. %frequency and amplitude
  2. fs=200; %sampling freq.
  3. dt =1/fs;
  4. N=fs/200%number of samples/cycle
  5. m=6 %no. of cycles
  6. t =dt*(0:400); %dt*(0:m*N); %data window
  7. fi=50; %Frequency test
  8. ww=wgn(201,1,-40);
  9. size(transpose(ww))
  10. t =dt*(0:200);
  11. y=sin(2*pi*fi*t + 0.3);
  12. v=sin(2*pi*fi*t + 0.3)+transpose(ww);
  13. tmax=1;
  14. % v : as function of time
  15. % fs : sampling frequency (Hz)
  16. % tmax : time of final estimation
  17. % to test: [t,f]=ZC(@(t)(220*sin(2*pi*50.1*t+pi/2)),50*512,1)
  18. % to test: [t,f]=ZC(@(t)(220*sin(2*pi*50.1*t+pi/2)+randn(1)*.1),50*512,1)
  19. dt=1/fs;
  20. v_old=v(1);
  21. i_max=tmax*fs;
  22. if(v_old==0)
  23. old_cross=0;
  24. else
  25. old_cross=-1;
  26. end
  27. new_cross_detected=0;
  28. t_est=[];
  29. f_est=[];
  30. for i=200:1:400
  31. v_new=v(i.*dt);
  32. if(v_old.*v_new<0)
  33. new_cross=(i-1+v_old/(v_old-v_new))*dt;
  34. new_cross_detected=1;
  35. elseif(v_new==0)
  36. new_cross=i*dt;
  37. new_cross_detected=1;
  38. end
  39. if(new_cross_detected==1)
  40. if(old_cross~=-1)
  41. t_est=[t_est;i*dt];
  42. f_est=[f_est;1/(2*(new_cross-old_cross))];
  43. end
  44. old_cross=new_cross;
  45. new_cross_detected=0;
  46. end
  47. v_old=v_new;
  48. end
  49. t_est
  50. f_est
  51. plot(t_est,f_est,'red')
  52. o=rms(fi)
  53. c=rms(f_est)
  54. RMSE = sqrt(mean(c - o).^2)
  55. hold on
  56. xlabel('time')
  57. ylabel('frequency')
  58. title('ZC white noise')
  59. plot (t,fi)
  60. hold off
Steven Lord
Steven Lord 2020년 6월 13일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by