Efficient Matrix Multiplication with Nested Functions for MATLAB Homework

In summary, the code attempts to solve the homework equation, but fails because of a dimension mismatch.
  • #1
Feodalherren
605
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]
------------------------------
[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[/FONT]
------------------------

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
  • #2
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 Feodalherren
  • #4
Nevermind, figured it out. Thanks!
 

Related to Efficient Matrix Multiplication with Nested Functions for MATLAB Homework

1. What are nested functions in MATLAB?

Nested functions in MATLAB refer to functions that are defined within another function. These functions can only be accessed and used within the parent function, and cannot be called from outside of it.

2. How do I create a nested function in MATLAB?

To create a nested function in MATLAB, you simply need to define it within the body of another function. The nested function must be preceded by the keyword "function" and must be indented within the parent function.

3. Can I call a nested function from outside of the parent function?

No, you cannot call a nested function from outside of the parent function. Nested functions are only accessible within the scope of the parent function.

4. What are the advantages of using nested functions in MATLAB?

Nested functions can help to organize and structure code, as they are only accessible within the parent function. They can also improve performance, as they can access and modify variables from the parent function without having to pass them as arguments.

5. Are there any limitations to using nested functions in MATLAB?

One limitation of nested functions in MATLAB is that they cannot be used in parallel computing. This means that if you need to perform computations simultaneously, you will need to use a different approach.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
16
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
924
  • Engineering and Comp Sci Homework Help
Replies
1
Views
885
  • Programming and Computer Science
Replies
1
Views
925
  • Engineering and Comp Sci Homework Help
Replies
2
Views
867
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
826
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
Back
Top