Loops to create an array (matlab)

In summary, the conversation involved a student seeking help with creating an array using loops. The expert provided a code template and explained how to use loops to add elements to the array. The student had difficulty understanding and the expert provided a more simplified solution. The conversation ended with the student successfully creating the desired array.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


Attached image

Homework Equations


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:
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:
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.
 

Attachments

  • 5.2.jpg
    5.2.jpg
    26.4 KB · Views: 961
Last edited:
Physics news on Phys.org
  • #2
Basically I thought just to have an array with 5x5 dimensions and use loops to add elements to the array with the numbers desired.
... 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:
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:
  • #3
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
 
  • #4
I edited the prev post to get you a hint while you were replying ;)
 
  • #5
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?
 
  • #6
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:
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
 
  • #7
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:
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:

What is an array in Matlab?

An array in Matlab is a data structure that can hold multiple elements of the same data type. It is similar to a list or vector in other programming languages.

How do I create an array in Matlab?

To create an array in Matlab, you can use the square brackets [ ] notation and separate each element with a comma. For example, [1, 2, 3] would create an array with three elements.

What is the purpose of using loops to create an array in Matlab?

Using loops to create an array in Matlab allows you to efficiently create arrays with a large number of elements. It also allows you to perform operations on each element of the array without having to write out each line of code individually.

What types of loops can be used to create an array in Matlab?

The two main types of loops that can be used to create an array in Matlab are the for loop and the while loop. The for loop is useful for creating arrays with a specific number of elements, while the while loop is useful for creating arrays with varying numbers of elements based on certain conditions.

Can I use nested loops to create a multidimensional array in Matlab?

Yes, you can use nested loops to create a multidimensional array in Matlab. This involves using one loop to iterate through the rows and another loop to iterate through the columns of the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
736
  • Engineering and Comp Sci Homework Help
Replies
3
Views
696
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
950
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
788
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
Back
Top