1. Not finding help here? Sign up for a free 30min 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!

Loops to create an array (matlab)

  1. Jun 21, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    Attached image


    2. Relevant equations



    3. The attempt at a solution
    I am basing my code off a template I found in a textbook for using loops to create an array. If I didn't find this example I don't think I would even know where to begin with this. Here is what I came up with so far

    Code (Text):
    levelVals = [10, 4, 7];
    n = 2*size(levelVals,2) - 1;
    m = 2*size(levelVals,2) - 1;
    boxArray = zeros(n,m);
    for k = 1:n
        for h = 1:m
            if k == levelVals(1)
                boxArray(k,h) = k;
            elseif h == 1
                boxArray(k,h) = levelVals(1);
            else
                boxArray(k,h) = levelVals(2);
            end
        end
    end
    boxArray;
    Here is boxArray
    Code (Text):
    boxArray =

        10     4     4     4     4
        10     4     4     4     4
        10     4     4     4     4
        10     4     4     4     4
        10     4     4     4     4
    Basically I thought just to have an array with 5x5 dimensions and use loops to add elements to the array with the numbers desired.
     

    Attached Files:

    • 5.2.jpg
      5.2.jpg
      File size:
      26.4 KB
      Views:
      127
    Last edited: Jun 21, 2014
  2. jcsd
  3. Jun 21, 2014 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    ... great, well done.
    Did you have a question?

    I notice the "boxArray" generated by your code is not like the one in the question attached.
    It is better to understand things than copy code off some other source.

    watch this:
    Code (Text):

    octave:4> a=10*ones(5)
    a =

       10   10   10   10   10
       10   10   10   10   10
       10   10   10   10   10
       10   10   10   10   10
       10   10   10   10   10

    octave:6> a(2:4,2:4)=5
    a =

       10   10   10   10   10
       10    5    5    5   10
       10    5    5    5   10
       10    5    5    5   10
       10   10   10   10   10

    octave:5> a(3,3)=7
    a =

       10   10   10   10   10
       10    5    5    5   10
       10    5    7    5   10
       10    5    5    5   10
       10   10   10   10   10
     
    ... so you just need some loops to do that, starting with a general vector.
     
    Last edited: Jun 21, 2014
  4. Jun 21, 2014 #3

    Maylis

    User Avatar
    Gold Member

    Yes, I am having difficulty making my code create boxArray to look like what it is supposed to look like. My question is how to write the for loop to make that array
     
  5. Jun 21, 2014 #4

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    I edited the prev post to get you a hint while you were replying ;)
     
  6. Jun 21, 2014 #5

    Maylis

    User Avatar
    Gold Member

    I don't understand what your 4, 6, 5 > a(..,..) is doing. What is that doing, and why those numbers? Also, do you suggest I incorporate if elseif else end statements nested in my for loop?
     
  7. Jun 21, 2014 #6

    Maylis

    User Avatar
    Gold Member

    This ended up doing it. I basically had to have the gsi hold my hand through the whole thing..this is frustrating as hell

    Code (Text):
    n = length(levelVals);
    boxArray = zeros(2*n-1,2*n-1);
    for i = 1:length(levelVals)
            boxArray(i:2*n-i,i) = levelVals(i);
            boxArray(i,i:2*n-i) = levelVals(i);
            boxArray(i:2*n-i,[i,2*n-i]) = levelVals(i);
            boxArray([i,2*n-i],i:2*n-i) = levelVals(i);
    end
     
  8. Jun 21, 2014 #7

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Well done.

    To explain what I did:

    you know that a(1,1) =5 will make the top-left element of a into a 5?
    then a(1:3,1)=5 will make the top three elements of the left-most row into 5's.
    and a(1,1:3)=5 will make the three leftmost elements of the top row into 5's...

    what I was doing was selecting the elements that needed to be changed as a block so there was no need to step through them like the gsi had you do.

    So you'd want to do something more like:
    Code (Text):

    clear
    levelVal=[10,5,7]; %% an arbitrary input vector
    n=length(levelVals); s=2*n+1;

    for k= 1:n
      boxArray(k:s-k+1,k:s-k+1)=levelVals(k);
    end
    boxArray
     
    ... the last "boxArray" is to provide an output.

    In the above example,
    "clear" gives us a blank slate to work with.
    n=3 so s=5

    so k will go from 1 to 3.

    when k=1, s-k+1=5
    boxArray(1:5,1:5)=levelVal(1) will make a 5x5 array filled with 10's, and store it.

    when k=2, s-k+1=4
    boxArray(2:4,2:4)=levelVal(2) will make the middle 9 elements of the stored matrix into 5's
    ... see how this works? The square pattern is being built up like stacking blocks on top of each other.

    when k=3, s-k+1=3
    boxArray(3:3,3:3)=levelVal(3) will make the middle element into a 7.

    You can experiment with indexing a matrix to get a feel for how it works.
     
    Last edited: Jun 21, 2014
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: Loops to create an array (matlab)
  1. For loops in Matlab (Replies: 22)

  2. MatLab for loops (Replies: 3)

Loading...