1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB nested functions

  1. Apr 21, 2015 #1
    1. The problem statement, all variables and given/known data
    Untitled.png


    2. Relevant equations

    Code (Matlab M):

    ----------------------
    [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]
    ------------------------
     
    3. The attempt at a solution
    Code (Matlab M):

    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: Apr 22, 2015
  2. jcsd
  3. Apr 22, 2015 #2

    DrClaude

    User Avatar

    Staff: Mentor

    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?
     
  4. Apr 22, 2015 #3

    jedishrfu

    Staff: Mentor

  5. Apr 22, 2015 #4
    Nevermind, figured it out. Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: MATLAB nested functions
  1. MATLAB Function (Replies: 0)

Loading...