Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I make these two separate for loops into one nested for loop? When I try

ID: 3574935 • Letter: H

Question

How can I make these two separate for loops into one nested for loop? When I try, I keep getting dimesion error?

clc, clear

A = [1:11; 2:12; 3:13; 4:14; 5:15; 6:16; 7:17 ]

[M N] = size(A);

for i = 1:M

Ax = [A(i, N - 5 + 1:N) A(i, 1:N - 5)];

Ax(1,(1:5)) = 0;

k(i,:) = Ax;

end

for j = 1:N

Ay = [k(1+2:M,j); k(1:2,j)];

Ay(end-1:end) = 0;

k(:,j) = Ay;

end

k

The final output should be k, as in the picture below:

7891011 567891011 12 13 14 67891011 12 13 14 7891011 12 13 14 15 16 10 11 12 13 14 15 16 17 91011 12 1234567 890-200 1111111 111 0123456 7890100 1111111 11 9012345 6789000 111111 890-234 5678900 11111 7890-23 4567800 1111 6789012 3456700 111 5678901 0000000 11 4567890 0000000 3456789 0000000 2345678 0000000 1234567 0000000 A k

Explanation / Answer

A = [1:11; 2:12; 3:13; 4:14; 5:15; 6:16; 7:17 ]
[M N] = size(A);
C = M+N
for i = 1:C
if(i<=M)
Ax = [A(i, N - 5 + 1:N) A(i, 1:N - 5)];
Ax(1,(1:5)) = 0;
k(i,:) = Ax;
else
Ay = [k(1+2:M,i-M); k(1:2,i-M)];
Ay(end-1:end) = 0;
k(:,i-M) = Ay;
end
end

k

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote