How to do an iteration with for loop

8 views (last 30 days)
Jenjen Ahmad Zaeni
Jenjen Ahmad Zaeni on 3 May 2021
Edited: Arjun on 6 Dec 2024 at 9:43
Hello everyone. I have a code for doing a decoding iteration. I have problem with looping, and i've attached the .m file. Sorry for bad explanation, but i want the final value of variable, lets say it's
Lj = -41.0644 -5.4258 0 -39.2725 0
0 0 -40.9465 7.0998 0
0 -7.0998 0 0 40.9465
-11.1200 0 -39.2725 0 -5.4258
to be processed again for n times like the commands above it. So it's like the value of Lj is processed again like the value of Lb, which is the top variable of the loop. When i put ii=1:n, its value remain the same. I understand my loop is not really right. What should i fix to make it works? Thank you very much.

Answers (1)

Arjun
Arjun on 6 Dec 2024 at 9:43
Edited: Arjun on 6 Dec 2024 at 9:43
I see that you want to update the value of "Lj" to be processed everytime the loop runs an iteration.
To ensure that the iterative decoder processes the "Lj" variable correctly for n iterations, you need to adjust the loop so that the output of each iteration is used as the input for the next one. Currently, the loop is not updating the initial values correctly for each iteration.
Kindly refer to the modified version of the code for better understanding:
Lch = [-25.2552 -21.2350 -25.1373 -14.1352 15.8092]; % From Antenna
n = 9; % Doing Iteration n times
Lm = zeros(1, 20);
H = [1 1 0 1 0;
0 0 1 1 0;
0 1 0 0 1;
1 0 1 0 1];
% Initialize La2 and La
La2 = repmat(Lch, 4, 1); % Corrected replication
La = zeros(size(La2));
La = H .* La2;
for ii = 1:n
% Reshape La and initialize Lc
Lb = reshape(La', 1, []);
Lc = num2cell(Lb);
Lc(Lb == 0) = {[]};
% Calculate Le
Le = zeros(1, 20); % Initialize Le
for k = 1:4
for kk = 1+(k-1)*5:k*5
Ld = [Lc{setdiff(1+(k-1)*5:k*5, kk)}];
Le(kk) = sign(prod(Ld)) * min(abs(Ld));
end
end
% Reshape Le and calculate Lg
Lf = reshape(Le, 5, 4)';
Lg = H .* Lf;
idx = [4 3 1 2 1; 2 2 4 1 2; 3 1 3 3 4; 1 4 2 4 3];
[row, columns] = size(Lg);
for col = 1:columns
Lg(:, col) = Lg(idx(:, col), col);
end
% Calculate Li and Lj
Lh = repmat(Lch, 4, 1);
Li = Lh + Lg;
Lj = Li .* H;
% Update La for the next iteration
La = Lj;
end
disp('Final Lj:');
Final Lj:
disp(Lj);
-73.5055 45.3530 0 -73.5055 0 0 0 -73.3876 -60.5936 0 0 38.2532 0 0 73.5055 -71.7136 0 -71.7136 0 62.2676
  • The "La" variable is initialized outside the loop and updated at the end of each iteration with the current value of "Lj".
  • The "La" variable is used as the input for each iteration, ensuring that the output of one iteration becomes the input for the next.
  • The loop now correctly updates and uses the variables as intended for iterative processing.
  • This should allow your iterative decoder to process "Lj" for n iterations, updating its values in each step.
Kindly refer to the documentation of "for" loop to use it more effectively: https://www.mathworks.com/help/releases/R2021a/matlab/ref/for.html
I hope it helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!