Vectorization for Two Nested for-loops
Hello all,
I’m trying to vectorize some operation between two 2D matrices, A and B. Basically, the process is multiplying every row verctor from matrix B by every vector from matrix A and store the value in matrix C. The correct non-vectorized code looks as follows:
A = [2 3 4; 5 2 1; 1 4 3];
B = [3 4 4; 4 2 1; 4 4 1];
C = zeros(3,3); % Initializing C
for ii=1:size(A,1)
for jj=1:size(B,1)
AA = A(ii,:);
BB = transpose(B(jj,:));
C(jj,ii) = sum(BB*AA, ‘all’)
end
end
C
The resultant C should looks as:
C =
99 88 88
63 56 56
81 72 72
Can I get help here?
Thanks!Hello all,
I’m trying to vectorize some operation between two 2D matrices, A and B. Basically, the process is multiplying every row verctor from matrix B by every vector from matrix A and store the value in matrix C. The correct non-vectorized code looks as follows:
A = [2 3 4; 5 2 1; 1 4 3];
B = [3 4 4; 4 2 1; 4 4 1];
C = zeros(3,3); % Initializing C
for ii=1:size(A,1)
for jj=1:size(B,1)
AA = A(ii,:);
BB = transpose(B(jj,:));
C(jj,ii) = sum(BB*AA, ‘all’)
end
end
C
The resultant C should looks as:
C =
99 88 88
63 56 56
81 72 72
Can I get help here?
Thanks! Hello all,
I’m trying to vectorize some operation between two 2D matrices, A and B. Basically, the process is multiplying every row verctor from matrix B by every vector from matrix A and store the value in matrix C. The correct non-vectorized code looks as follows:
A = [2 3 4; 5 2 1; 1 4 3];
B = [3 4 4; 4 2 1; 4 4 1];
C = zeros(3,3); % Initializing C
for ii=1:size(A,1)
for jj=1:size(B,1)
AA = A(ii,:);
BB = transpose(B(jj,:));
C(jj,ii) = sum(BB*AA, ‘all’)
end
end
C
The resultant C should looks as:
C =
99 88 88
63 56 56
81 72 72
Can I get help here?
Thanks! vectorization MATLAB Answers — New Questions