Loop with row and column difference (multiplication)

I would like to know how I do a loop multiplying Q1 with O3 (result in Q3). Then multiply Q3 with O4 and so on for each column.
I tried to explain with other software to facilitate understanding. Thank you for your help.

댓글 수: 3

I asked a friend that knows a little of programming to code for me, but he never used matlab
what he explained to me:
T2 is my O colun (69x1)
dir_teta is the Q, R, S, etc... (36 coluns). (1x36)
aux_T2 he created to store the result (69x36)
he came up with this code, but It's still wrong
T2=E(:,1);
nrows_2 = length(T2);
ncols_2 = length(dir_teta);
aux_T2= ones(nrows_2,ncols_2);
for i=1:ncols_2
aux_T2(i,1)=dir_teta(1,i);
end
for j = 1:ncols_2
for i = 1:nrows_2-1
aux_T2(i+1,j)=aux_T2(i,j)*T2(i);
end
end
Did you look at my answer? First load your file and look at the data to see how it was loaded (how it is named). It would help if you attached your data file.
load('yourfile');

댓글을 달려면 로그인하십시오.

답변 (2개)

Cris LaPierre
Cris LaPierre 2020년 11월 10일

0 개 추천

Assuming you meant to include a Q2, I'd do this using cumprod instead of a for loop.
David Hill
David Hill 2020년 11월 10일
편집: David Hill 2020년 11월 10일
I assume your arrays when imported will start at 1. Looks like you just want cumprod() of O times initial Q.
Q(2:end)=Q(1)*cumprod(O);

댓글 수: 6

clear all; clc; close all
alpha=0.008;
gamma=3.3;
g=9.8;
delta_1=0.07;
delta_2=0.09; % if f>fp
Tp=[2 8 16];
freq_p=1./Tp;
i_freq=1:69;
f1=0.03093;
c=1.1;
for i = 1:length(i_freq)
fi=f1.*c.^(i_freq-1);
end
freq_fi=fi;
nrows = length(freq_fi);
ncols = length(freq_p);
E = ones(nrows,ncols);
fc = 0.4054; % [rad/s]
for i = 1:nrows
for j = 1:ncols
f = freq_fi(i);
fp = freq_p(j);
if f<fc
if f<=fp
E(i,j) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
else
E(i,j) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
end
elseif f>fc
if f<=fp
E(i,j) = (fc/f)^5*(alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
else
E(i,j) = (fc/f)^5*(alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
end
else
E(i,j)=0;
end
end
end
E;
%%% 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;
f_teta=teta_i;
%%%%%%%%%%%%% My problem below %%%%%%%%%%%%%%%%%%%%
%%% T=2s
T2=E(:,1);
T2(T2==0)=1;%changed all T2 values that were 0 to 1 (if you multiply by zero everything below will be zero)
%when I ran it without this, the whole 69x36 matrix was zeros
aux_T2=cumprod(T2)*dir_teta;
aux_T2.txt came out like this, but it would be according to the T2_.txt (done in excel) .
I made a figure for better understanding:
What do you do with the zero data within your table? What you are describing in red does not match your orginal question and is still confusing. Does Q4=Q1*O3*O4? That was how you described it. Or are you not placing the values into the array as you go such that Q4=orginal(Q3)*O4? What are you doing for all the white space above (between Q9 and Q12, R5 and R12, ...)?
Does Q4=Q1*O3*O4?
Yes.
It took me a lot of time to notice why to use cumprod(), but now I don't know how to write the code,
What I thought:
aux= cumprod(O);
being O, the Dir(alpha) array.
Now, I need do repeat this aux 69 times ( lenght of T2, that's E(f) in the figure)
and multiply each copy of aux with each value of T2
Will it work?
I managed to do it using the code:
clear all; clc; close all
alpha=0.008;
gamma=3.3;
g=9.8;
delta_1=0.07;
delta_2=0.09; % if f>fp
Tp=[2 8 16];
freq_p=1./Tp;
i_freq=1:69;
f1=0.03093;
c=1.1;
for i = 1:length(i_freq)
fi=f1.*c.^(i_freq-1);
end
freq_fi=fi;
nrows = length(freq_fi);
ncols = length(freq_p);
E = ones(nrows,ncols);
fc = 0.4054; % [rad/s]
for i = 1:nrows
for j = 1:ncols
f = freq_fi(i);
fp = freq_p(j);
if f<fc
if f<=fp
E(i,j) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
else
E(i,j) = (alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
end
elseif f>fc
if f<=fp
E(i,j) = (fc/f)^5*(alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_1*fp))^2);
else
E(i,j) = (fc/f)^5*(alpha*(g^2)*((2*pi)^(-4))*(f^(-5)))*exp((-5/4)*((f/fp)^(-4)))*gamma*exp((-1/2)*((f-fp)/(delta_2*fp))^2);
end
else
E(i,j)=0;
end
end
end
E;
%%% 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;
f_teta=teta_i;
%%%%%%%%%%%%% My problem below %%%%%%%%%%%%%%%%%%%%
%%% T=2s
T2=E(:,1);
T2=T2';
aux1=cumprod(dir_teta');
aux2=repmat(aux1,1,69);
aux3=aux2.*T2;
Thank you for your help David Hill
And I'm sorry for the confusion I made.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 11월 10일

댓글:

2020년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by