"Subscript indices must either be real positive integers or logicals." again! See codes
188 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Star Strider
le 10 Jan 2015
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 commentaires
Plus de réponses (3)
Stephen23
le 10 Jan 2015
Modifié(e) : Stephen23
le 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.
4 commentaires
Stephen23
le 10 Jan 2015
True, [] simply returns an empty array. I have edited my answer to reflect this.
Luca
le 21 Août 2017
Modifié(e) : Walter Roberson
le 21 Août 2017
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 commentaires
Walter Roberson
le 21 Août 2017
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
le 11 Juin 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));
- function [t_est,f_est]=threeDFT(v,fs,tmax,N0)
- % v : volt as function of time
- % fs : sampling frequency (Hz)
- % tmax : time of final estimation
- % N0 : number of samples in the window
- % to test: [t,f]=threeDFT(@(t)(220*sin(2*pi*50.1*t+pi/2)),50*512,1,512)
- % to test: [t,f]=threeDFT(@(t)(220*sin(2*pi*50.1*t+pi/2)+randn(size(t))*.1),50*512,1,512)
- fs=50*512; %sampling freq.
- dt =1/fs;
- N0=fs/50; %number of samples/cycle
- m=50; %no. of cycles
- t = dt*(0:m*N0); %data window
- fi=50; %Frequency test
- ww=wgn(201,1,-40);
- size(transpose(ww))
- t =dt*(0:200);
- y=sin(2*pi*fi*t + 0.3);
- v=sin(2*pi*fi*t + 0.3)+transpose(ww)
- tmax=1;
- n=N0-1:-1:0;
- f0=50;
- f=50.88;
- Hc=2/N0*cos(2*pi*n/N0+pi/N0);
- Hs=-2/N0*sin(2*pi*n/N0+pi/N0);
- t_est=[];
- f_est=[];
- j_max=tmax*fs;
- for j=1:j_max+1
- x=v(((j-1):(j+ N0-2))*(dt));
- c(j)=x*Hc';
- s(j)=x*Hs';
- if(j>N0)
- Ac(j-N0)=sqrt(sum(c(end-N0+1:end).^2)/N0);
- As(j-N0)=sqrt(sum(s(end-N0+1:end).^2)/N0);
- cc(j-N0)=c(end-N0+1:end)*Hc';
- ss(j-N0)=c(end-N0+1:end)*Hs';
- if(j>2*N0)
- Acc(j-2*N0)=sqrt(sum(cc(end-N0+1:end).^2)/N0);
- Ass(j-2*N0)=sqrt(sum(ss(end-N0+1:end).^2)/N0);
- ccc(j-2*N0)=cc(end-N0+1:end)*Hc';
- ccs(j-2*N0)=cc(end-N0+1:end)*Hs';
- ssc(j-2*N0)=ss(end-N0+1:end)*Hc';
- sss(j-2*N0)=ss(end-N0+1:end)*Hs';
- 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);
- t_est=[t_est;(j-1)*dt];
- f_est=[f_est;ff];
- end
- end
- end
- t_est;
- f_est
- plot(t_est, f_est,'red')
- hold on
- RMSE = sqrt(mean((f_est-fi).^2))
- xlabel('time')
- ylabel('frequency')
- title('3LDFT WHITE NOISE ')
- plot (t,fi)
- hold off
3 commentaires
common fernando
le 12 Juin 2020
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);
>>
- %frequency and amplitude
- fs=200; %sampling freq.
- dt =1/fs;
- N=fs/200%number of samples/cycle
- m=6 %no. of cycles
- t =dt*(0:400); %dt*(0:m*N); %data window
- fi=50; %Frequency test
- ww=wgn(201,1,-40);
- size(transpose(ww))
- t =dt*(0:200);
- y=sin(2*pi*fi*t + 0.3);
- v=sin(2*pi*fi*t + 0.3)+transpose(ww);
- tmax=1;
- % v : as function of time
- % fs : sampling frequency (Hz)
- % tmax : time of final estimation
- % to test: [t,f]=ZC(@(t)(220*sin(2*pi*50.1*t+pi/2)),50*512,1)
- % to test: [t,f]=ZC(@(t)(220*sin(2*pi*50.1*t+pi/2)+randn(1)*.1),50*512,1)
- dt=1/fs;
- v_old=v(1);
- i_max=tmax*fs;
- if(v_old==0)
- old_cross=0;
- else
- old_cross=-1;
- end
- new_cross_detected=0;
- t_est=[];
- f_est=[];
- for i=200:1:400
- v_new=v(i.*dt);
- if(v_old.*v_new<0)
- new_cross=(i-1+v_old/(v_old-v_new))*dt;
- new_cross_detected=1;
- elseif(v_new==0)
- new_cross=i*dt;
- new_cross_detected=1;
- end
- if(new_cross_detected==1)
- if(old_cross~=-1)
- t_est=[t_est;i*dt];
- f_est=[f_est;1/(2*(new_cross-old_cross))];
- end
- old_cross=new_cross;
- new_cross_detected=0;
- end
- v_old=v_new;
- end
- t_est
- f_est
- plot(t_est,f_est,'red')
- o=rms(fi)
- c=rms(f_est)
- RMSE = sqrt(mean(c - o).^2)
- hold on
- xlabel('time')
- ylabel('frequency')
- title('ZC white noise')
- plot (t,fi)
- hold off
Steven Lord
le 13 Juin 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
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!