Index exceeds the number of array elements
이전 댓글 표시
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
Davide Frey
2019년 1월 28일
You are raising the index "i" by 3 at every loop, so on the last loop it can exceed the size of array "data".
Btw do you really need to increase the size of "k" by one at every loop? This is automatically done by a for-loop...
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.
채택된 답변
추가 답변 (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
댓글 수: 3
madhan ravi
2019년 1월 28일
Ask a separate question with all the details required.
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?
Rik
2020년 11월 11일
doc diff
Suganthi D
2022년 3월 15일
편집: Walter Roberson
2022년 3월 15일
0 개 추천
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
Walter Roberson
2022년 3월 15일
f1tmp(km)=elec_price(km)*(pdis_charget(km)/1e3)*delt; % objective function 1
That code would have a problem if elec_price or pdis_charget are arrays and km is greater than the number of elements in the array.
Perhaps you wanted multiplication?
f1tmp(km)=elec_price*(km)*(pdis_charget*(km)/1e3)*delt; % objective function 1
I would also suspect it might be
f1tmp(km)=elec_price*(km) + (pdis_charget*(km)/1e3)*delt; % objective function 1
but it is difficult to be sure of any of this without more of the code to look at.
Walter Roberson
2022년 5월 9일
r1 is not defined in that code. Maybe it is empty.
dugasa getachew
2022년 5월 9일
편집: dugasa getachew
2022년 5월 9일
oh sorry it is defined above let me show full code
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
Walter Roberson
2022년 11월 28일
Which line is the problem occuring on?
We do not know what inputs you used.
I suspect that you have an existing variable in your workspace that is named one of axis or plot or roots or title or xlabel or ylabel
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. ');
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]);
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!