40 likes | 154 Vues
In this lecture, we explore the concept of nested loops through the lens of matrix multiplication in scientific programming. We provide a code snippet that demonstrates how to multiply two matrices element-wise using nested loops. The exercise challenges students to write a function that computes the product of two matrices (not element-wise) using nested loops. Initially, we can assume the matrices are the correct size, but ultimately, the function should handle any matrix sizes, ensuring robustness. Join us as we deepen our understanding of nested loops and their applications in matrix operations.
E N D
COMP 116: Introduction to Scientific Programming Lecture 18: Nested loops and review
Nested loops: what does this code do? % Assume A and B are pre-assigned % matrices and of the same size C=zeros(size(A)); for i=1:size(A,1) for j=1:size(A,2) C(i,j)=A(i,j)*B(i,j) end end
Exercise • Write a function that • given two matrices • returns their product (matrix multiplication not element-wise multiplication) • Use nested for loops • For the first version of the code you can assume that the input matrices are the right size • For the final version, make no such assumption