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 commentaires

Davide Frey
Davide Frey le 28 Jan 2019
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
Rik le 28 Jan 2019
What is your goal with this code? It might be possible to do this in a much faster and cleaner way.
rpid
rpid le 28 Jan 2019
Modifié(e) : rpid le 28 Jan 2019
I need to construct this:
x(i) h(k)=sum(x(i):x(i+2))
1
2
3 6
4
5
6 24
7
8
9 42
10
11
12 60
13
14
15 78
16
17
18 51
19
20
21 60
22
23
24 69
25
26
27 78
28
29
30 87
I need a sequence as presented in h(k)=sum(x(i):x(i+2))...
The 2nd column is a sum of three elements of the 1st column.

Connectez-vous pour commenter.

 Réponse acceptée

madhan ravi
madhan ravi le 28 Jan 2019
Modifié(e) : madhan ravi le 28 Jan 2019
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

Plus de réponses (6)

madhan ravi
madhan ravi le 28 Jan 2019
Modifié(e) : madhan ravi le 28 Jan 2019
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

1 commentaire

rpid
rpid le 28 Jan 2019
Thank you! It It works perfectly.
Thank you all for attention.

Connectez-vous pour commenter.

Rik
Rik le 28 Jan 2019
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.

2 commentaires

rpid
rpid le 28 Jan 2019
Thank you. It works perfectly also.
rpid
rpid le 28 Jan 2019
Thank you. It works perfectly also.

Connectez-vous pour commenter.

madhan ravi
madhan ravi le 28 Jan 2019
Modifié(e) : madhan ravi le 28 Jan 2019
x=1:30;
Result=squeeze(sum(reshape(x,3,1,[]))) % without transpose
rpid
rpid le 28 Jan 2019
Modifié(e) : rpid le 28 Jan 2019
Thank you again. All answers worked.
I just don't know how can I apply for a sequence not divisible by other numbers. I have a sequence of 1118 elements and I need to sum the same sequence in 3 to 3, 6 to 6, 9 to 9, 12 to 12, 18 to 18, 24 to 24, 48 to 48...
The same procedure, but applied in these:
x=1:30;
Result=squeeze(sum(reshape(x,3,1,[]))) % without transpose
I have the input data a matrix 1:1118 and 2 columns.

3 commentaires

madhan ravi
madhan ravi le 28 Jan 2019
Ask a separate question with all the details required.
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?
doc diff

Connectez-vous pour commenter.

Suganthi D
Suganthi D le 15 Mar 2022
Modifié(e) : Walter Roberson le 15 Mar 2022

0 votes

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 commentaires

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.
r1 is not defined in that code. Maybe it is empty.
dugasa getachew
dugasa getachew le 9 Mai 2022
Modifié(e) : dugasa getachew le 9 Mai 2022
oh sorry it is defined above let me show full code
dugasa getachew
dugasa getachew le 9 Mai 2022
Modifié(e) : dugasa getachew le 9 Mai 2022
% 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
Rik le 9 Mai 2022
@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.

Connectez-vous pour commenter.

¿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 commentaires

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
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. ');
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. ');
Index exceeds the number of array elements. Index must not exceed 3.

'disp' appears to be both a function and a variable. If this is unintentional, use 'clear disp' to remove the variable 'disp' from the workspace.
% 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]);

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by