Index exceeds the number of array elements
조회 수: 11 (최근 30일)
이전 댓글 표시
Dear all. How can I fix this: "Index exceeds the number of array elements"?
data=[1:30]';
x=data(:,1);
n=length(data);
for i=1:n-2
for k=1:n-2
h(k)=sum(data((i):(i+2)));
k=k+1;
i=i+3;
end
end
Thank you all!
댓글 수: 3
Rik
2019년 1월 28일
What is your goal with this code? It might be possible to do this in a much faster and cleaner way.
채택된 답변
madhan ravi
2019년 1월 28일
편집: madhan ravi
2019년 1월 28일
One way:
x=(1:30).';
[m,n]=size(x);
N=3;
Result=zeros(m/N,n); % preallocate
for k = 1:m/N
Result(k,n)=sum(x(k*N-(N-1):k*N));
end
Result
Gives:
Result =
6
15
24
33
42
51
60
69
78
87
댓글 수: 0
추가 답변 (6개)
madhan ravi
2019년 1월 28일
편집: madhan ravi
2019년 1월 28일
Another way:
x=(1:30).';
[m,n]=size(x);
N=3;
C=mat2cell(x,repmat(N,1,m/N));
Result=cellfun(@sum,C)
Gives:
Result =
6
15
24
33
42
51
60
69
78
87
Rik
2019년 1월 28일
This would also work:
x=(1:30)';
Result=sum(reshape(x,3,[]))';
The transposes are not needed, but are only there to keep input and output shape consistent with the other methods by Madhan.
madhan ravi
2019년 1월 28일
편집: madhan ravi
2019년 1월 28일
x=1:30;
Result=squeeze(sum(reshape(x,3,1,[]))) % without transpose
댓글 수: 0
rpid
2019년 1월 28일
편집: rpid
2019년 1월 28일
댓글 수: 3
Prasanth Warrier
2020년 11월 11일
I have an array created with this command
t = [0:0.01:0.99 1:0.1:9.9 10:1:100];
this creates 281 columns
I want to know difference between each columns
like time interval between 2nd and 1st colulmn and between 2nd and 3rd like wise.
How do i go about it?
Suganthi D
2022년 3월 15일
편집: Walter Roberson
2022년 3월 15일
good Morning professors,
can anyone help me to sort out this problem..
Index exceeds the number of array elements (127).
Error in fitness_process2_12 (line 83)
f1tmp(km)=elec_price(km)*(pdis_charget(km)/1e3)*delt; % objective function 1
Error in
minlp_process_12>@(x)fitness_process2_12(x,EVSE1_arr_time,EVSE1_leave_time,EVSE2_arr_time,EVSE2_leave_time,EVSE3_arr_time,EVSE3_leave_time,finaltime,socintij1,socintij2,socintij3,data_pass)
(line 14)
func=@(x) fitness_process2_12(x,EVSE1_arr_time,EVSE1_leave_time,...
Error in fmincon (line 567)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in minlp_process_12 (line 29)
final_best_value=fmincon(func,(intial_solu),adatamat,bdatamat,[],[],lower_lmt,upper_lmt,[],options); %%Call fmincon ----
with the fval output to obtain the value of the objective function at the solution.
Error in MARCH (line 61)
[finalres]=minlp_process_12(min_val1,max_val1,EVSE1_arr_time,EVSE1_leave_time,...
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
댓글 수: 5
dugasa getachew
2022년 5월 9일
편집: dugasa getachew
2022년 5월 9일
% Measure obje performance of the (GA based parameter optimization)
function IEA= EvalObj(x)
th=x(1); dis=x(2);interDis=x(3);fr=x(4);
radius = 0.31;
interRadius = 0.015;
omega = 2*pi*fr;
permVac = 4*pi* 10^(-7) ;
permRel = 1500;
perm = permVac*permRel;
cond = 10.1^6;
Current = 10; % Coil Currnet
alpha = 10000;
Lamb =235;
h = 11;
T = 200 ;
Ta=25;
domain = [0 0.29 0 th];
r1= interRadius: interDis:radius;
alpha1 = @(x,r,z) sqrt(x.^2 + 1j*omega*perm*cond) ;
num = @(x,r,z) (exp(-alpha1(x,r,z).*(th-z)) .*(alpha1(x,r,z)-x) + exp( alpha1(x,r,z).*(th-z)) .*(alpha1(x,r,z) + x));
den = @(x,r,z) exp(alpha1(x,r,z).*th).*(alpha1(x,r,z) + x).^2 - exp(-alpha1(x,r,z).*th) .*(alpha1(x,r,z)-x).^2;
H = 0;
IEA=0;
for i = 1:28
tic
i
f = @(x,r,z) besselj(1,x*r1(i)).*besselj(1,x.*r).*exp(-x*dis).*x;
Temp = @(r,z) sum(chebfun(@(x) f(x,r,z) .*num(x,r,z) ./ den(x,r,z), [ 0 alpha]));
Flux = chebfun2(@(r,z) perm*Current*r1(i)*Temp(r,z), domain,'vectorize on');
H = H + Flux;
toc
J= 1j*omega*cond*H;
Ind =abs(J);
x = chebfun2(@(x,y) x, domain)
N = chebop2(@(x,y,u) Lamb.*x.*diffx(u,2) + Lamb.*diffx(u,1) + Lamb.*x.*diffy(u,2) , domain);
N.lbc =@(x,u) diff(u);
N.rbc = @(x,u) diff(u,1) +h*(u - Ta) ./Lamb; % z- direction
N.ubc= @(x,u) diff(u,1) +h*(u - Ta) ./Lamb; % r - direction
N.dbc = @(x,u) diff(u);
s = x.*Ind;
u=N \-s;
%u=u(:,domain(4));
IEA=IEA+(u-T)/T;
end
%%%%%%%%%%%%%%%%
Index exceeds the number of array elements (1).
Error in EvalObj>@(r,z)perm*Current*r1(i)*Temp(r,z) (line 28)
Flux = chebfun2(@(r,z) perm*Current*r1(i)*Temp(r,z), domain,'vectorize
on');
what is problem i tried many times but couldn't get the solution
Rik
2022년 5월 9일
@dugasa getachew: have a read here and here. Then post your question to a separate thread. It will greatly improve your chances of getting an answer.
Jaqueline Hernández
2022년 11월 28일
¿Cómo puedo solucionar esto: "El índice supera el número de elementos de la matriz"?
disp ('introducir valores en S.I. ');
b= input('¿cual es el ancho del canal? ');
yl=input('¿cual es el valor de yl? ');
disp ('dz positivo si es ascendente ');
dz=input ('¿cual es el valor de dz? ');
Q= input('¿cual es el valor del gasto? ');
Al= b*yl;
vl_2=(Q/Al)^2;
cvl=vl_2/(2*9.81);
El=yl+cvl;
Ea= El-dz
a=(Q^2/(2*9.81*b^2));
r=roots([1,-Ea,0,a])
y=(.0000001:.01:(2*y1));
E= y+(Q^2./((2*9.81)*(b*y).^2));
plot (E,y,Ea,r,'rd',El,y1,'go')
axis([0 5 0 5])
title ('GRAFICA E vs y')
xlabel ('Energia, E (m)')
Ylabel ('Tirante, y (m)')
grid
댓글 수: 3
Jaqueline Hernández
2022년 11월 28일
En la primera linea me marca el error que el indice supera el numero de matrices
disp ('introducir valores en S.I. ');
DGM
2022년 11월 28일
As Walter mentioned, check that you don't have a variable called disp.
% disp is a function
disp('introducir valores en S.I. ');
% disp is now a variable (e.g. a numeric array)
disp = [1 2 3];
% now you're trying to index into an array using a character vector
% note that the linter will now highlight this line since it's an
% addressing expression with no output
disp('introducir valores en S.I. ');
% which is implicitly handled as numeric indices
disp([105 110 116 114 111 100 117 99 105 114 32 118 97 108 111 114 101 115 32 101 110 32 83 46 73 46 32]);
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!