How to iterate matrix multiple times?

2 次查看(过去 30 天)
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 个)

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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by