Loops to create an array (matlab)

Click For Summary

Discussion Overview

The discussion revolves around creating a specific array structure in MATLAB using loops. Participants are addressing a homework problem that involves generating a 5x5 array based on certain values and conditions, with a focus on the implementation of nested loops and array indexing.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant shares their initial code for creating an array and expresses uncertainty about how to proceed without a template.
  • Another participant points out that the generated array does not match the expected output and suggests understanding the underlying concepts rather than copying code.
  • A participant asks for clarification on specific array indexing techniques used by another, expressing confusion about the numbers involved.
  • One participant describes their successful implementation of the array creation, detailing how they adjusted their approach based on guidance from others.
  • Another participant explains their method of using block selection in array indexing to simplify the process of filling the array with values.

Areas of Agreement / Disagreement

There is no consensus on a single correct approach, as participants present different methods and techniques for achieving the desired array structure. Some express frustration with the process, while others provide alternative strategies.

Contextual Notes

Participants reference specific array dimensions and values, but there are unresolved questions about the best practices for using loops and indexing in MATLAB. The discussion highlights various assumptions about the expected output and the effectiveness of different coding strategies.

gfd43tg
Gold Member
Messages
949
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,064
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 1 ·
Replies
1
Views
1K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K