How to fix the error :Index exceeds matrix dimensions
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jacqueline Rigatto
el 11 de Nov. de 2020
Comentada: Jacqueline Rigatto
el 13 de Nov. de 2020
clear all; clc; close all
rho_sea=1027;
rho_air=1225;
g=9.8;
k=0.4;
alpha=0.008;
gamma=3.3;
delta_1=0.07;
delta_2=0.09;
Tp=[2 10 16];
fp=1./Tp;
T=2:1:70;
f_T=1./T;
u= [1.192 1.194 1.120];
z=[0.0021 0.0029 0.0033];
sigma= 2*pi*f_T;
k_nw=sigma.^2./g;
i_freq=1:69;
f1=0.03093;
c=1.1;
for i = 1:length(i_freq)
fi=f1.*c.^(i_freq-1);
end
%%% Dir(teta)
i_teta=1:36;
teta_i=10.*i_teta;
teta_i_cos=cos(2*teta_i);
for i = 1:length(teta_i_cos)
if teta_i_cos>0
dir_teta=0;
else
dir_teta= (2/pi())*(1/2)*(1+teta_i_cos);%cos^2(x) = (1+cos2x)/2
end
end
dir_teta;
%% T=2s
%mi
mi_2 = zeros(numel(teta_i),numel(sigma));
for i=1:length(sigma)
for j=1:length(teta_i)
mi_2(i,j)=(((g*z(1,1).*(k_nw(i).^2))./sigma(i).^2).*exp((k*sigma(i))./(k_nw(i).*u(1,1)).*cos(teta_i(j))));
end
end
mi_2;
mi_2(:,[37:69])=[];
% mp
mp_2=(1.2./k).*mi_2.*log(mi_2).*log(mi_2).*log(mi_2).*log(mi_2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%% My problem below %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%beta
beta_2= zeros(numel(sigma),numel(teta_i));
Cf=(g.*T)./(2.*pi);
for i=1:length(sigma)
for j=1:length(teta_i)
beta_2(i,j)=(rho_air./rho_sea).*mp_2(i,j).*((u(1,1))./(Cf(i))).^2.*(1+cos(teta_i(i).*2)./2);
end
end
My mistake is on this line (third line from the bottom of the code):
beta_2(i,j)=(rho_air./rho_sea).*mp_2(i,j).*((u(1,1))./(Cf(i))).^2.*(1+cos(teta_i(i).*2)./2);
Thank you for your help
4 comentarios
Walter Roberson
el 12 de Nov. de 2020
i_teta=1:36;
teta_i=10.*i_teta;
so teta_i is length 36.
T=2:1:70;
That is length 69
f_T=1./T;
so that is length 69
sigma= 2*pi*f_T;
and so is sigma.
for i=1:length(sigma)
for j=1:length(teta_i)
so i ranges from 1 to 69 and j ranges from 1 to 36
beta_2(i,j)=(rho_air./rho_sea).*mp_2(i,j).*((u(1,1))./(Cf(i))).^2.*(1+cos(teta_i(i).*2)./2);
^^^^^^^^^
i is at most 69. Consider in particular when i becomes 37. teta_i(i) would be teta_i(37) . But teta_i only has 36 elements.
Cf=(g.*T)./(2.*pi);
with T being length 69, Cf has 69 elements, so indexing Cf(i) with i to 69 is correct.
mi_2 = zeros(numel(teta_i),numel(sigma));
mi_2 is going to be initialized as 36 by 69.
for i=1:length(sigma)
i is going to be 1 to 69
for j=1:length(teta_i)
j is going to be 1 to 36
mi_2(i,j)=(((g*z(1,1).*(k_nw(i).^2))./sigma(i).^2).*exp((k*sigma(i))./(k_nw(i).*u(1,1)).*cos(teta_i(j))));
so you are going to assign up to mi_2(69, 36) when m_2 was initialized to 36 by 69. When you assign explicitly to a row or column that is larger than the existing size of the matrix, then the matrix is automatically expanded, so with the row index being up to 69, m_2 is going to expand up to 69 rows, giving you an m_2 that is 69 by 69 with the columns 37 and beyond left at their initial values of 0.
for i=1:length(sigma)
for j=1:length(teta_i)
so i ranges from 1 to 69 and j ranges from 1 to 36
beta_2(i,j)=(rho_air./rho_sea).*mp_2(i,j).*((u(1,1))./(Cf(i))).^2.*(1+cos(teta_i(i).*2)./2);
^^^^^^^^^
so you are going to access up to mp_2(69, 36) which is the part of mp_2 that had data written into it. That will be okay, but it does lead to the question of why you set up mp_2 to be 69 x 69 when you never use columns 37 to 69 from it.
=====
My recommendation would be that you rewrite your code to not use the variable names i or j as you are obviously losing track of what they mean. I suggest that you consider using names such as sigmaidx and thetaidx which would make it much clearer which variable has which role.
Respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!