Efficient Matrix Multiplication with Nested Functions for MATLAB Homework

  • Thread starter Thread starter Feodalherren
  • Start date Start date
  • Tags Tags
    Functions Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB homework problem focused on implementing efficient matrix multiplication using nested functions. Participants explore issues related to code execution, function definitions, and readability.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant shares their MATLAB code for matrix multiplication and a transpose function, noting that the code produces no output when executed.
  • Another participant questions the placement of the calculations for A1 and A2, suggesting that it may be incorrectly positioned within the code.
  • A participant expresses confusion regarding the implementation of the MyTranspose function, specifically why the variable B is used on both sides of the assignment.
  • One participant provides a link to MATLAB documentation on nested functions, indicating that it may offer helpful insights.
  • A suggestion is made to improve code readability by adding proper indentation to the function bodies.
  • A later reply indicates that the original poster resolved their issue independently.

Areas of Agreement / Disagreement

The discussion includes multiple viewpoints regarding the implementation of the functions and code structure. There is no consensus on the best approach, as participants raise different concerns and suggestions.

Contextual Notes

Participants express uncertainty about specific coding practices and the behavior of the provided functions. There are unresolved questions about the logic and structure of the code, particularly concerning the MyTranspose function.

Feodalherren
Messages
604
Reaction score
6

Homework Statement


Untitled.png

Homework Equations


[/B]
Matlab:
----------------------
[FONT=Courier New]function [C]=mymatmult(A,B)
[L1 C1]=size(A);
[L2 C2]=size(B);

if C1 ~= L2
  error('dimension mismatch');
end %if ERROR

C=zeros(L1,C2);
for i=1:L1
  for j=1:C2
  C(i,j)=A(i,:)*B(:,j);
  end %in for
end %out for
end %function
------------------------------
[FONT=Courier New]function B = MyTranspose(A)

[rows cols] = size(A);

B=zeros(cols,rows);

for i=1:rows
  for j=1:cols
  B(j,i) = A(i,j)+B(j,i);
  end %in for
end %out fo

end %function
------------------------

The Attempt at a Solution


Matlab:
function [A1, A2] = MyTransposeProduct(A)

%place your nested function here--------------------------
  function B = MyTranspose(A)

  [rows, cols] = size(A);

  B=zeros(cols,rows);

  for i=1:rows
  for j=1:cols
  B(j,i) = A(i,j)+B(j,i);
  end %end in for
  end %end out for
  end %end in funct

%end nested function------------------------------------------

%second nested function---------------------------------------

  function [C]=mymatmult(A,B)
  [L1, C1]=size(A);
  [L2, C2]=size(B);

  if C1 ~= L2
  error('dimension mismatch');
  end %if ERROR

  C=zeros(L1,C2);
  for i=1:L1
  for j=1:C2
  C(i,j)=A(i,:)*B(:,j);
  end %in for
  end %out for

  A1=mymatmult(A,MyTranspose(A));
  A2=mymatmult(MyTranspose(A),A);  end %function

%end second nested function-----------------------------------

end % main function
But nothing happens when I run this code. No errors and no result...
 
Last edited by a moderator:
Physics news on Phys.org
Look where you put the code to calculate A1 and A2.

Also, I don't understand why you wrote MyTranspose that way. Why does B appear on both sides of the assignment?
 
  • Like
Likes   Reactions: Feodalherren
Nevermind, figured it out. Thanks!
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K