Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab: Generate matrix

  1. Jun 15, 2012 #1

    sharks

    User Avatar
    Gold Member

    I have attached the problem to this post. My attempt at the first part (i used 'm' instead of 'l' as it's less confusing, since the latter resembles the digit '1') and here is my script:

    Code (Text):
    A = zeros(10);
    for k=1:10
      for  m=1:10
        A(k,m)  =  sin(k)*cos(m);
      end
    end
     
    A
    The answer:

    A =

    0.4546 -0.3502 -0.8330 -0.5500 0.2387 0.8080 0.6344 -0.1224 -0.7667 -0.7061
    0.4913 -0.3784 -0.9002 -0.5944 0.2579 0.8731 0.6855 -0.1323 -0.8285 -0.7630
    0.0762 -0.0587 -0.1397 -0.0922 0.0400 0.1355 0.1064 -0.0205 -0.1286 -0.1184
    -0.4089 0.3149 0.7492 0.4947 -0.2147 -0.7267 -0.5706 0.1101 0.6895 0.6350
    -0.5181 0.3991 0.9493 0.6268 -0.2720 -0.9207 -0.7229 0.1395 0.8737 0.8046
    -0.1510 0.1163 0.2766 0.1826 -0.0793 -0.2683 -0.2107 0.0407 0.2546 0.2344
    0.3550 -0.2734 -0.6504 -0.4294 0.1864 0.6308 0.4953 -0.0956 -0.5986 -0.5513
    0.5346 -0.4117 -0.9795 -0.6467 0.2806 0.9500 0.7459 -0.1440 -0.9014 -0.8301
    0.2227 -0.1715 -0.4080 -0.2694 0.1169 0.3957 0.3107 -0.0600 -0.3755 -0.3458
    -0.2939 0.2264 0.5386 0.3556 -0.1543 -0.5224 -0.4101 0.0792 0.4957 0.4565

    which i hope is correct?

    But i have no idea how to do the same thing with a loop-free script.
     

    Attached Files:

    Last edited: Jun 15, 2012
  2. jcsd
  3. Jun 15, 2012 #2

    sharks

    User Avatar
    Gold Member

    Here is my attempt for part 2 but i think there is a shorter version:

    k=sin(1:1:10);
    m = cos(1:1:10);
    r1=k(1).*m
    r2=k(2).*m
    r3=k(3).*m
    r4=k(4).*m
    r5=k(5).*m
    r6=k(6).*m
    r7=k(7).*m
    r8=k(8).*m
    r9=k(9).*m
    r10=k(10).*m
    A=[r1;r2;r3;r4;r5;r6;r7;r8;r9;r10]
     
  4. Jun 15, 2012 #3
    Well, they seem to agree with my Mathcad-generated results, if that's of some reassurance.


    Try something like:
    k = 1:10;
    A = sin(k)' * cos(k);

    note the single quote mark, the transpose operator. The creates a vector, k, with values 1 to 10, the sin and cos functions operate over vectors, resulting in 2 vectors, the sin is transposed and the resulting matrix multiplication is equivalent to the first loop method.

    You can use just k as the number of k and m elements are the same.
     

    Attached Files:

  5. Jun 15, 2012 #4
    Forgot to add, image showing proof of pudding in Mathcad.
     

    Attached Files:

  6. Jun 15, 2012 #5

    sharks

    User Avatar
    Gold Member

    That's an ingenious way of solving it, as a 10x1 matrix multiply by another 1x10 matrix gives the required 10x10 matrix! :smile:

    Thank you very much, NemoReally.
     
  7. Jun 15, 2012 #6
    It is, isn't it. :smile:


    The important thing about languages such as Mathcad, Mathematica, Maple, Matlab and J is learning to get one's head out of the detailed programming gutter and looking up into the higher level world of mathematics, particularly arrays and functions. There are still occasions when a bit of close quarters coding is required, but its far better to be able to think about multiplying two matrices at company commander level rather than march them around the binary parade square yourself. (I now it declare it International Mixed Metaphor Day.) Imagine you're writing the problem down on a whiteboard and then see what methods exist to support what you've written.

    No worries.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab: Generate matrix
Loading...