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

Matlab: Matricies and Arrays

  1. Jun 30, 2011 #1
    Hi, I'm new to MATLAB and I have a problem:

    I am trying to create a multiplication matrix of sorts.

    The first row will have numbers from 0 to 1 in steps of .0101
    The first column will have the same, from 0 to 1 in steps of .0101
    Then to generate the rest I will multiple each entry in the column by their corresponding row.
    For example, the 2nd column, 2nd row entry will be .0101 * .0101
    It will look like this:
    0 .0101 .0202 ............ 1
    .0101 (.0101 * .0101) (.0101 * .0202) ......... 1
    .
    .
    .
    1 ( 1 * .0101) (1 * .0202) ........... 1

    The problem is, I dont know how to write a code to do this. I'm sure it involves a loop of some sort with indexing from 1 to 100. Can someone help me?

    I thought this might help somewhere:
    indexfinal=99;
    0:1/indexfinal:1;
    Indeed, it gives me the steps of .0101 I'm looking for.
    I just dont know how to make it so that I can create a matrix like the aboveformentioned.
     
    Last edited: Jun 30, 2011
  2. jcsd
  3. Jul 1, 2011 #2

    MATLABdude

    User Avatar
    Science Advisor

    The great thing about MATLAB is that you can use vectorization (operations on vectors, or 1-D arrays) to speed things up. It doesn't do anything in parallel (I think) but it should go faster than if you had to loop everything using a double loop.

    Do you know how to use the colon operator to extract / work with an entire row / column? If you had some m x n matrix (m rows by n columns) named foo, you could extract just the 2nd row (to, say, vector bar) by typing in the following:
    >> bar = foo(2, :)

    Or if you wanted the 3rd column, you'd type in:
    >> bar = foo(:, 3)

    You can also mix and match and select just the subset of rows 2-5, and columns 3-7:
    >> bar = foo(2:5, 3:7)

    Now, here's most of the way to the code you're looking for (comments follow the '%' symbol, effective until you start a new line):

    Code (Text):

    % Row / column headings
    indexfinal=99;
    headings=0:1/indexfinal:1;

    % Generate each row by multiplying the first row by the first column
    subTable=zeros(indexfinal+1);
    magicTable(1,:)=headings;   %first row
    for n=2:indexfinal+1
        magicTable(n, :)=headings * headings(n);
    end

    % Fix first column
     
    Fixing the first column is left as an exercise to the reader. You should probably also read the Mathworks' page on indexing:
    http://www.mathworks.com/company/newsletters/articles/Matrix-Indexing-in-MATLAB/matrix.html

    As well as the MATLAB documentation page on looping (mostly 'for' and 'while') and conditionality (mostly 'if'):
    http://www.mathworks.com/help/techdoc/learn_matlab/f4-1931.html [Broken]

    As you can see, very comprehensive documentation (along with examples) is available at the Mathworks' website.

    EDIT: You may need to make use of the transpose operator (the apostrophe symbol). It's here under list of arithmetic operations:
    http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html
     
    Last edited by a moderator: May 5, 2017
  4. Jul 2, 2011 #3
    I tried doing something like

    indexfinal=99;
    span = 0:1/indexfinal:1;
    row(2,:) = span;
    column(:,2) = span;

    for n=2:indexfinal+1;
    for m=2:indexfinal+1;
    row(n,:) = span*span(n);
    column(:,m) = span*span(m);
    table = row * column;
    end
    end

    But it still doesn't give me the desired result, and doesn't even work at all...
    Right now I'm getting an error saying column(:,2) = span; has an assignment dimension mismatch, but I dont see why.

    And this isn't even half of what I'm really doing. The reason I need the table the way it is is because I'm modeling the diffusion of water and I'm trying to use the table to represent values of C for 0 < C < 1 where I have to solve something like...
    C(i,t) = dt*D(i)*[(C(i+1,t) + C(i-1,t) - 2*C(i,t))/(dx*dx)];
    I'm supposed to take dt = 0.1 and dx = 1. D is just an array where every element is 0.001. Then I have to solve the equation somehow for values of C between 0 and 1. I think i and t act as some kind of index while the equation represents an approximation with derivatives.

    Ideally, C is close to 0 near the center of the table, i.e. row 50 column 50, or around there, and closer to 1 at the far ends of the table.

    I've been having such a hard time with this, I just can't seem to understand matlab at all...
     
    Last edited: Jul 2, 2011
  5. Jul 3, 2011 #4

    MATLABdude

    User Avatar
    Science Advisor

    There are two issues with your first few lines of code (where you create and try to fill row and column).

    Firstly, you can't just invoke a matrix and then have partially filled or zero-padded values. You also can't resize these non-filled matrices (as you implicitly attempt to do). To make what you've got work (I think), you need to first do something like:
    Code (Text):
    row=zeros(2, prod(size(span));
    This creates a matrix named row that's filled with zeros, and has 2 rows by however big span happens to be. The reason I take the product of the result of size is that regardless of whether it's a row or column vector, the product will always be the number of elements in the vector.

    Now, there may be a logical error in your algorithm in that the second command would (with the above) fill the second row of matrix row with the vector span. I call it a vector, but it should really be called a 1 x whatever matrix.
    Code (Text):
    row(2,:) = span;
    This brings me to the second problem, the reason you're getting the span error. span is a 1 x whatever matrix. You're attempting to fill a column (size: whatever x 1) with a row (size: 1 x whatever). So in order to make this work, you need to use the transpose (the apostrophe operator, ') of span:
    Code (Text):
    column(:,2) = span';
    This all said, MATLAB like any coding language, can be crashed and banged very to produce something that's functional. I don't mean to patronize, but it sounds like this may be what you're trying to do since you don't have some of the basics (e.g. indexing) in place. The following tutorials (video or written--I prefer the written myself) will help fill in some of the blank spots:
    http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

    Before MATLABdude was MATLABdud, so you may have to follow the following algorithm:
    1) code something
    2) have it fail, bang head against wall / keyboard
    3) look up the documentation via help <command> and through the documentation site
    4) goto 1)

    Good luck!

    EDIT: Huh, having re-read my first post, it seems as if I already suggested the tutorials / documentation--I confused your post with another one that I answered at the same time, but where I'd forgotten to put in the tutorial suggestion!
     
    Last edited: Jul 3, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab: Matricies and Arrays
  1. Matlab and arrays (Replies: 0)

  2. Arrays in MATLAB (Replies: 3)

Loading...