MATLAB trouble with reshape and random matricies

In summary, the speaker is struggling with two assignments in their MATLAB course from MIT OCW. One involves creating a 10x10 matrix with the numbers 1-100 running down the columns, and the other involves creating a 5x3 matrix with random integers ranging from -3 to 3. They eventually figure out how to use the reshape and rand functions in MATLAB to complete the assignments. They express gratitude for any help and mention that they are learning solely from the MIT slides.
  • #1
JonDrew
64
0
I am trying to learn MATLAB with MIT OCW and I am running into some trouble.

It says as an assignment:

c. cMat = a 10x10 matrix where the vector 1:100 runs down the columns (use reshape).

so

1 11 21...91
2....92
. .....
10...100

is the matrix I am trying to make and another exercise says

f. Make fMat be a 5x3 matrix of random integers with values on the range -3 to 3 (use rand and floor or ceil)

I am not sure how it wants me to go about these, thanks for any help. The course link is listed below.

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-094-introduction-to-matlab-january-iap-2010/index.htm
 
Physics news on Phys.org
  • #3
Figured it out. It took me a while though, sadly.

for c. I used

cMat= linspace(1, 100)
cMat= reshape(cMat,10, 10)

and d. I used

fMat= ceil(rand(5,3)*10);
mod(fMat, 4)

And I really appreciate the help I am learning it through the MIT slides alone, I find I get hung up on little things like this.
 

Related to MATLAB trouble with reshape and random matricies

1. Why am I getting an error when trying to reshape my matrix in MATLAB?

There could be a few reasons for this error. It could be that the dimensions you are trying to reshape the matrix into do not match the original dimensions. It could also be that the matrix is not in the correct format for reshaping, such as being a vector instead of a matrix. Double check your dimensions and the format of your matrix.

2. How can I generate a random matrix in MATLAB?

To generate a random matrix in MATLAB, you can use the "rand" function. This function creates a matrix of random values between 0 and 1. You can also use other variations of this function, such as "randn" for a matrix of normally distributed random values or "randi" for a matrix of random integers.

3. Why is my randomly generated matrix not changing each time I run the code?

By default, MATLAB uses the same seed value every time it runs. This means that the same random matrix will be generated each time you run the code. To change this, you can use the "rng" function to specify a different seed value each time the code is run. This will result in a different randomly generated matrix each time.

4. How can I reshape a multidimensional matrix in MATLAB?

To reshape a multidimensional matrix in MATLAB, you can use the "reshape" function. This function allows you to specify the new dimensions for the matrix. You can also use the "permute" function to change the order of the dimensions before reshaping.

5. Is there a way to specify the range of values for a randomly generated matrix in MATLAB?

Yes, there are a few ways to specify the range of values for a randomly generated matrix in MATLAB. One way is to use the "rand" function and then multiply the matrix by the desired range. For example, if you want a matrix with values between 10 and 20, you can use the code "10 + 10*rand(3,3)". Alternatively, you can use the "randi" function and specify the range in the function's parameters.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
28K
Back
Top