How to iterate matrix multiple times?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jenjen Ahmad Zaeni
el 16 de En. de 2022
Comentada: Jenjen Ahmad Zaeni
el 16 de En. de 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 comentarios
Respuesta aceptada
KSSV
el 16 de En. de 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
Más respuestas (1)
Simon Chan
el 16 de En. de 2022
Editada: Simon Chan
el 16 de En. de 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
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!