Matlab Problem structured array

In summary: Board(rows,cols)%creates a structured array with hyphens at each locationYour game board has rows * cols cells. This means that there should be .1 * rows * cols cells with zebras, the same number of giraffes, .05*rows*cols hyenas, and 1 lion.For example, if there are 30 rows and 40 columns (1200 cells), there should be 120 zebras, 120 giraffes, 60 hyenas, and 1 lion.One way to distribute the animals is to generate .25 * rows * cols random numbers in the range 1 through rows * cells. Using
  • #1
gpax42
25
0
Not sure if anyone here is familiar with MATLAB programming. I am supposed to create a structured array (resembling a game board) that acts as a predator/prey simulator. The user needs to specify the size of the game board. Within the gameboard I am supposed to represent a couple of different animals. Zebras, represented with a 'Z' should occupy 10% of the gameboard, giraffes represented with a 'G' occupy another 10%, hyenas represented with an 'H' occupy 5%, and there will be 1 lion represented with an 'L'. Unoccupied spaces will be represented with a hyphen.

this is what I've come up with so far. I tried making the board filled with hyphens, and then replacing them by specifying different animals for random locations within the board. There might be an easier way to do this using vectorization and not for loops. I am also confused about how I should make each animal occupy random locations that amount to a certain percent of the gameboard. How can I do this randomly without having the same locations within the array specified twice? Once agian, vectorization might offer an easier means to doing this, I am just not sure how.

Any help you can offer will be greatly appreciated!

function board = makeBoard(rows,cols)

for rowIndex = 1:rows
for colIndex = 1:cols
board(rowIndex,colIndex).type = '-';
end
end

%creates a structured array with hyphens at each location
 
Physics news on Phys.org
  • #2
Your game board has rows * cols cells. This means that there should be .1 * rows * cols cells with zebras, the same number of giraffes, .05*rows*cols hyenas, and 1 lion.

For example, if there are 30 rows and 40 columns (1200 cells), there should be 120 zebras, 120 giraffes, 60 hyenas, and 1 lion.

One way to distribute the animals is to generate .25 * rows * cols random numbers in the range 1 through rows * cells. Using the example of a 30 row by 40 column board, you would generate 400 numbers in the range 1 through 1200. The first 120 of these you could distribute as zebras, the next 120 as giraffes, and the last 60 as hyenas. To make the explanation easier to understand, I'll continue using 30 rows and 40 columns. I'm saving the lion for last.

Supposing that one of the random numbers generated was 723. This can be converted to row and col numbers by using the rem (short for remainder) function. rem(723, 30) gives the remainder after dividing 723 by 30, which is 3, and this will be the column number. If you subtract 3 from 723, you get 720, and 720 divided by 30 is 24, so an animal whose random number is 723 should be placed in row 24, column 3.

Do that for all the animals except the lion. I don't think it's likely that you'll get a repeated random number, so this algorithm shouldn't end up with two animals at the same location, but I suppose it's possible, so you should check that where you put an animal has a '-' character in it. If it's some other character, you'll need to figure out some other place to put that animal.

When you have all the zebras, giraffes, and hyenas in place, generate one more random number in the range 1 through rows * cols. Figure out as already described the cell of the array to put the lion. If there's already another animal there, generate another random number, and continue doing this until you get a cell that doesn't already have an animal in it.
 
  • #3
thanks for the great advice :smile:...

I wrote out the code for the algorithm. My first for loop looks something like this...

function board = makeBoard(rows,cols)

for rowIndex = 1:rows
for colIndex = 1:cols
board(rowIndex,colIndex).type = '-';
end
end

for index = 1:.1*rows*cols
randomNumber = randi([1,rows*cols]);
colNumber = rem(randomNumber,rows);
rowNumber = abs(randomNumber - colNumber);
board(rowNumber,colNumber).type = 'Z';
end

my only problem occurs when I run the code. A Matlab error prompt appears reading...

? Cell contents indices must be greater than 0

Error in ==> makeBoard at 13
board(rowNumber,colNumber).type = 'Z';

I tried fixing this by adding the built in MATLAB absolute value command when subtracting my randomNumber by colNumber, but this didnt seem to help
 
  • #4
You might be having a problem with this line:
randomNumber = randi([1,rows*cols]);
I don't think this is the right syntax for this function, particularly the part with the thing in brackets.

Also, before this line
board(rowNumber,colNumber).type = 'Z';

add some code to display the values of rowNumber and colNumber. One or both of these is not a positive integer.
 
  • #5
i think i got it to work correctly... here's my code...
-----------------------------------------------------------
function board = makeBoard(rows,cols)

for rowIndex = 1:rows
for colIndex = 1:cols
board(rowIndex,colIndex).type = '-';
end
end

for index = 1:.1*rows*cols
randomNumber = randi([1,rows*cols]);
colNumber = rem(randomNumber,rows);
if (colNumber == 0)
colNumber = colNumber + 1;
end
rowNumber = rem(randomNumber - colNumber,rows);
if (rowNumber == 0)
rowNumber = rowNumber + 1;
end
board(rowNumber,colNumber).type = 'Z';
end

for index = 1:.1*rows*cols
randomNumber = randi([1,rows*cols]);
colNumber = rem(randomNumber,rows);
if (colNumber == 0)
colNumber = colNumber + 1;
end
rowNumber = rem(randomNumber - colNumber,rows);
if (rowNumber == 0)
rowNumber = rowNumber + 1;
end
board(rowNumber,colNumber).type = 'G';
end

for index = 1:.05*rows*cols
randomNumber = randi([1,rows*cols]);
colNumber = rem(randomNumber,rows);
if (colNumber == 0)
colNumber = colNumber + 1;
end
rowNumber = rem(randomNumber - colNumber,rows);
if (rowNumber == 0)
rowNumber = rowNumber + 1;
end
board(rowNumber,colNumber).type = 'H';
end

randomNumber = randi([1,rows*cols]);
colNumber = rem(randomNumber,rows);
if (colNumber == 0)
colNumber = colNumber + 1;
end
rowNumber = rem(randomNumber - colNumber,rows);
if (rowNumber == 0)
rowNumber = rowNumber + 1;
end
board(rowNumber,colNumber).type = 'L';
end

-----------------------------------------------------------

My only problem now is when I run my test function, I keep getting an error prompt indicating that my board has improper dimensions.

------------------------------------------------------------
function [] = test_makeBoard()
board = makeBoard(10,10);
err(size(board,1) ~= 10 || size(board,2) ~= 10,'board not created as 10x10');
err(~isa(board,'struct'),'board is not a struct array');
g=0;l=0;h=0;z=0;
for ii=1:100
board = makeBoard(10,10);
b = [board.type];
g=g+sum(b=='G');
l=l+sum(b=='L');
h=h+sum(b=='H');
z=z+sum(b=='Z');
end
err(g/10000 < .05 || g/10000 > .15,sprintf('giraffes at %d percent of board',g/10000));
err(z/10000 < .05 || z/10000 > .15,sprintf('zebras at %d percent of board',z/10000));
err(h/10000 < .02 || h/10000 > .08,sprintf('hyenas at %d percent of board',h/10000));
err(l ~= 100,'lions not one per board');
end
-----------------------------------------------------------

any idea why i might be getting this error message. I am pretty sure it works because when I run fprintf(board.type) at the end of my program, it shows that I have a 10X10 struct type array
 
  • #6
From what I can tell, your board array is just an array, but you're treating it as if it were an array of structures. I can do this in C or C++, but I don't know MATLAB well enough to create an array whose elements are structures. The best I can do is to point you to the MATLAB documentation (if you don't already have it or have a link to it).
Structures - http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-42340.html#f16-53937

I think what you might need to do is create a structure with all of the member names you plan to use, then create your board array, initializing each element in board with the structure.
Here's a link to a doc page that might be helpful - http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brd4btr.html

On the other hand, why do you have an array of structures? It appears to me that all you are putting in each cell is '-', 'Z', 'G', or 'L'. I don't see the need for the type member. You could just as well store these characters in your board array. I might be missing something here, that you are planning to have some other member value in each board element.
 
Last edited by a moderator:

What is a structured array in Matlab?

A structured array in Matlab is a data structure that allows you to store and manipulate data with different types and sizes in a single variable. It is similar to a table or spreadsheet, with rows and columns, and each element can hold a different type of data, such as numbers, strings, or even other arrays.

How do I create a structured array in Matlab?

To create a structured array in Matlab, you can use the "struct" function, followed by the field names and values you want to assign to each element. For example, "myArray = struct('Name',{'John', 'Mary', 'Bob'}, 'Age', [25, 30, 40])" will create a structured array with 3 elements, each with a "Name" and "Age" field.

Can I access and manipulate specific elements in a structured array?

Yes, you can access and manipulate specific elements in a structured array using dot notation. For example, to access the "Name" field of the second element in the array created in the previous question, you can use "myArray(2).Name". You can also use loops and other built-in functions to manipulate the elements in a structured array.

How is a structured array different from a cell array in Matlab?

A structured array is different from a cell array in Matlab in that it has a fixed number of elements and each element has a defined structure, whereas a cell array can have a variable number of elements and each element can hold any type of data. Additionally, structured arrays offer more efficient indexing and manipulation capabilities.

What are some common applications of structured arrays in Matlab?

Structured arrays are commonly used in Matlab for data processing and analysis tasks, such as storing and organizing experimental data, creating databases, and building data models. They are also useful for creating and manipulating complex data structures, such as graphs and networks, in scientific and engineering applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Programming and Computer Science
Replies
1
Views
1K
  • STEM Academic Advising
Replies
13
Views
2K
  • General Discussion
Replies
11
Views
25K
Back
Top