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 件のコメント

Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 10 日
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
David Hill
David Hill 2020 年 11 月 10 日
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');
Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 10 日
My script

サインインしてコメントする。

回答 (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.

1 件のコメント

Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 12 日
Thank you for your help Cris LaPierre

サインインしてコメントする。

David Hill
David Hill 2020 年 11 月 10 日
編集済み: David Hill 2020 年 11 月 10 日

0 投票

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 件のコメント

David Hill
David Hill 2020 年 11 月 10 日
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;
Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 11 日
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:
David Hill
David Hill 2020 年 11 月 11 日
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, ...)?
Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 11 日
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?
Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 11 日
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
Jacqueline Rigatto
Jacqueline Rigatto 2020 年 11 月 11 日
And I'm sorry for the confusion I made.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops 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