How to iterate matrix multiple times?

3 ビュー (過去 30 日間)
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2022 年 1 月 16 日
コメント済み: Jenjen Ahmad Zaeni 2022 年 1 月 16 日
Hello everyone. I have a 3x7 matrix
La =
-5.2622 3.2610 1.0999 0 2.0590 0 0
0 0 1.0999 -2.4128 0 -0.1168 0
-5.2622 0 0 -2.4128 0 0 9.2321
I have a program below
H = [1 1 1 0 1 0 0;
0 0 1 1 0 1 0;
1 0 0 1 0 0 1];
Lb=num2cell(La);
Lb(La==0) = {[]};
Lc=reshape(Lb',1,[]);
Lf=zeros(1,21);
for R = 1:3
for L = 1+(R-1)*7:R*7;
Le=[Lc{setdiff(1+(R-1)*7:R*7, L)}];
Lf(L)=sign(prod(Le))*min(abs(Le));
end
end
Lg=reshape(Lf,7,3)';
Lh=H.*Lg;
Li=H.*[sum(Lh([2 3],:)); sum(Lh([1 3],:)); sum(Lh([1 2],:))];
Lj=La+Li
The obtained result is
Lj =
-7.6750 3.2610 1.2167 0 2.0590 0 0
0 0 -0.9591 -7.6750 0 -0.1168 0
-4.1623 0 0 -2.5296 0 0 9.2321
Lj is the new value of La after being processed. I want Lj to be reprocessed again just like La, for 5 times. So there will be different value of Lj everytime it being processed. I have tried something, such as adding
for i = 1:5
%do something
end
but seems like it's not worked because i got the same value for every i-th. I think there are mistake when i tried the looping.
How is the correct way to do that? Thank you very much.

採用された回答

KSSV
KSSV 2022 年 1 月 16 日
La = [ -5.2622 3.2610 1.0999 0 2.0590 0 0
0 0 1.0999 -2.4128 0 -0.1168 0
-5.2622 0 0 -2.4128 0 0 9.2321] ;
H = [1 1 1 0 1 0 0;
0 0 1 1 0 1 0;
1 0 0 1 0 0 1];
iwant = zeros(3,7,5) ;
for i = 1:5
Lb=num2cell(La);
Lb(La==0) = {[]};
Lc=reshape(Lb',1,[]);
Lf=zeros(1,21);
for R = 1:3
for L = 1+(R-1)*7:R*7
Le=[Lc{setdiff(1+(R-1)*7:R*7, L)}];
Lf(L)=sign(prod(Le))*min(abs(Le));
end
end
Lg=reshape(Lf,7,3)';
Lh=H.*Lg;
Li=H.*[sum(Lh([2 3],:)); sum(Lh([1 3],:)); sum(Lh([1 2],:))];
Lj=La+Li ;
La = Lj;
iwant(:,:,i) = Lj ;
end
iwant
  1 件のコメント
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2022 年 1 月 16 日
It really works! Thank you very much, i really appreciate it.

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

その他の回答 (1 件)

Simon Chan
Simon Chan 2022 年 1 月 16 日
編集済み: Simon Chan 2022 年 1 月 16 日
Write your code as a function and save it as a m-file (or use the attached file):
function Lj = processLa(La)
H = [1 1 1 0 1 0 0;
0 0 1 1 0 1 0;
1 0 0 1 0 0 1];
Lb=num2cell(La);
Lb(La==0) = {[]};
Lc=reshape(Lb',1,[]);
Lf=zeros(1,21);
for R = 1:3
for L = 1+(R-1)*7:R*7
Le=[Lc{setdiff(1+(R-1)*7:R*7, L)}];
Lf(L)=sign(prod(Le))*min(abs(Le));
end
end
Lg=reshape(Lf,7,3)';
Lh=H.*Lg;
Li=H.*[sum(Lh([2 3],:)); sum(Lh([1 3],:)); sum(Lh([1 2],:))];
Lj=La+Li;
end
And then execute the following:
La=[-5.2622 3.2610 1.0999 0 2.0590 0 0;
0 0 1.0999 -2.4128 0 -0.1168 0;
-5.2622 0 0 -2.4128 0 0 9.2321];
%
for k = 1:5
La = processLa(La);
end
  1 件のコメント
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni 2022 年 1 月 16 日
It works too. I will doing this way in the future project. I really appreciate it. Thank you very much!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by