Hello I\'m writing a nested for loop to do element wise multiplication in a matr
ID: 2268407 • Letter: H
Question
Hello I'm writing a nested for loop to do element wise multiplication in a matrix and I keep getting a wonky output. What should get is a 2 x 2 matrix of [2,3;4,5]; but entries 4 and 5 end up being zeros for some reason here's the code thanks and this is written for matlab.
clear all
clc
clear all
D = ones(2,2);
E = [2,3;4,5];
F1 = zeros(2,2);
sizeD = D;
sizeE = E;
for i = 1:sizeD(1,:);
for j = 1:sizeE(1,:);
for i1 = 1:sizeD(:,1);
for j1 = 1:sizeE(:,1);
F1(i,j) = E(i,j)* D(i1,j1);
end
end
end
end
disp('F = ');
disp(F1);
%Output
F =
2 3
0 0
Explanation / Answer
E = [2 3; 4 5]; D = ones(2,2); F1 = zeros(2,2); for i1 = 1:2 for i2 = 1:2 for i3 = 1:2 F1(i1,i2) = F1(i1,i2) + D(i1,i3)*E(i3,i2); end; end; end; disp('F='); disp(F1);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.