Loops to create an array (matlab)

AI Thread Summary
The discussion focuses on using loops in MATLAB to create a specific array structure based on given values. The initial code provided creates a 5x5 array but does not match the desired output, prompting questions about the correct implementation of loops and indexing. A more effective approach is suggested, utilizing matrix indexing to fill the array in a layered manner, allowing for a more concise and efficient solution. The final code example demonstrates how to build the array by modifying specific blocks based on the input values. Understanding matrix indexing is emphasized as key to achieving the desired array configuration.
gfd43tg
Gold Member
Messages
947
Reaction score
48

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: 1,037
Last edited:
Physics news on Phys.org
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:
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
 
I edited the prev post to get you a hint while you were replying ;)
 
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?
 
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
 
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:

Similar threads

Replies
0
Views
163
Replies
0
Views
1K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
21
Views
3K
Replies
3
Views
1K
Replies
10
Views
3K
Replies
6
Views
2K
Replies
6
Views
2K
Back
Top