How to iterate matrix multiple times?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jenjen Ahmad Zaeni
am 16 Jan. 2022
Kommentiert: Jenjen Ahmad Zaeni
am 16 Jan. 2022
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.
0 Kommentare
Akzeptierte Antwort
KSSV
am 16 Jan. 2022
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
Weitere Antworten (1)
Simon Chan
am 16 Jan. 2022
Bearbeitet: Simon Chan
am 16 Jan. 2022
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
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!