Unsure of how to use and understand rand function

3 views (last 30 days)
Sami
Sami on 10 Feb 2014
Answered: Iain on 10 Feb 2014
This is for an Intro to Matlab Course I have to take.
the instructions state to use the rand function, create an array called A of size 1000 by 1000.
So i think it would be done by "A= RAND(1000,1000)
It then says:
Imagine that this array is made of up 4 500X500 arrays corresponding to the upper left
array, upper right array, lower left array, and lower right array. Using the appropriate
function calls,find the mean, median, and standard deviation of the elements making up
each of these four arrays. Report and comment on these values in your response section.
(Hint: read through the documentation for each of these functions. If you these functions a
2D array, what will the dimensions of the output be? You will likely want to use the
reshape() function in conjunction with median and standard deviation).
And I have NO CLUE what to do right here. Can anyone help me out, and guide me in a direction?

Answers (3)

Image Analyst
Image Analyst on 10 Feb 2014
Hint:
subArray = array(row1:row2, column1:column2);
meanOfSubArray = mean(subArray(:));
I imagine you know what row1, row2, column1, and column2 would be. For row2 and column2 being at the end, you can use "end" instead of 1000, e.g
subArray = array(501:end, 501:end);
  2 Comments
Sami
Sami on 10 Feb 2014
Sorry, I really don't understand what you did. So is my rand function correct as
A=rand(1000,1000)
And so for the second part, why would I set the columns to 1000.
Image Analyst
Image Analyst on 10 Feb 2014
I didn't say that. I said you could set column2 to be 1000 or "end" when you are indexing, specifically the right half. OK, another major hint . Here is how to get the upper right part of the array
upperRight = A(1:500, 501:end)
meanOfUpperRight = mean(upperRight(:));
The code to get the means of the other quadrants is very similar.

Sign in to comment.


Jos (10584)
Jos (10584) on 10 Feb 2014
It is always insightful to look at a smaller example:
A = [ 4 2 3 1 ; 5 4 2 3 ; 6 7 8 9 ; 1 4 2 5] % just 2-by-2 instead of 1000-by-1000
B = A(1:2,3:4) % which quadrant of A is now stored in B?
C = B(:) % how this does change the contents from B?
mean(C)

Iain
Iain on 10 Feb 2014
A= RAND(1000,1000)
Does like you think. It creates a 1000 x 1000 matrix.
The rest of the problem is trying to get you to think of a matrix just a table, a table which can be split up arbitrarily. A 3 x 3 table could be split up into 3, 1 x 3 vectors. A 4 x 4 table could be split up into 4, 2x2 tables.
It is telling you to split up a 1000 x 1000 into 4 500x500 tables.
It is then telling you to calculate the mean, median, and standard deviation of each of those 4 sections. e.g.
a_split_up_bit = A( 4:5,3:9);
Takes the 4th and 5th rows, on the 3rd to 9th columns, and puts them in a_split_up_bit as a 2x7 table.
"mean" (median/std, min, max, etc.) calculates the mean (mediant/standard deviation, min, max, etc) for any vector, not a table. It calculates the mean of each column in a table.
Reshape allows you to copy & reshape a table into a vector, or another, differently sized table.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!