• Support PF! Buy your school textbooks, materials and every day products Here!

MATLAB nested functions

  • #1
599
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:

Answers and Replies

  • #2
DrClaude
Mentor
7,339
3,520
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
599
6
Nevermind, figured it out. Thanks!
 

Related Threads on MATLAB nested functions

Replies
17
Views
10K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
5
Views
1K
  • Last Post
Replies
0
Views
5K
  • Last Post
Replies
5
Views
785
Replies
2
Views
2K
Replies
4
Views
12K
Top