[MATLAB] Subscripted assignment dimension mismatch.

In summary: Print the first 10 rows and first 10 columns:print(m(1:10,1:10));% Print the first 10 rows and all columns:print(m);
  • #1
hoheiho
47
0
Halo everyone, I am trying to consider a dice game using MATLAB. I used 'r=randi(6,a,b)' to get random number from MATLAB.Also run it for 200times.(a=a:1:x,b=b:1:y) Then used '[a,b]=mode(r)' to get the highest frequency value and add them together then put them in the table. For x=1 with y=1,2 or 3 is fine for my program coz its just 1x3 matrix. And now i want to get the x=2 with y=1,2 or 3 then the matrix will be 2x1,2x2 or 2x3. But what i want is just the red colour part in the table. Becuase if i used 2x1 matrix it will cause error for my 'sum' command so the arrey will not be balance and it case 'Subscripted assignment dimension mismatch'.
y
x 1 2 3
1 4.0 4.0 3.5
2 5.0 5.0 4.0
3 3.0 3.0 3.0

I have google it and it say i can use 'repmat' to build up a large matrix first, calculate the value 1 by 1 and then put it in the large matrix. But I am not quite understand how to use it. Is there anyone can give me a hand for this?
Thank you very much
 
Physics news on Phys.org
  • #2
!Yes, you can use the repmat() function in MATLAB to create a matrix of size x by y filled with random numbers between 1 and 6. You can then use the sum() function to calculate the sums of each row and column. Here is an example code that you can use:% Set the size of the matrix (x by y):x = 2;y = 3;% Create a matrix filled with random numbers between 1 and 6:r = randi(6,x,y);% Create a larger matrix of size x by y filled with zeros:m = repmat(0,x,y);% Calculate the sums of each row and column using the sum() functionfor i = 1:x for j = 1:y m(i,j) = sum(sum(r(1:i,1:j))); endend% Print out the matrix:m
 

FAQ: [MATLAB] Subscripted assignment dimension mismatch.

1. What does "subscripted assignment dimension mismatch" mean in MATLAB?

In MATLAB, "subscripted assignment dimension mismatch" refers to an error that occurs when the dimensions of the left and right sides of an assignment statement do not match. This means that the size of the matrix or array being assigned to does not match the size of the values being assigned to it.

2. How can I fix a subscripted assignment dimension mismatch error in MATLAB?

To fix a subscripted assignment dimension mismatch error in MATLAB, you can either adjust the dimensions of the values being assigned or the dimensions of the matrix or array being assigned to. This can be done by using functions such as reshape or repmat to change the size of the values, or by using functions such as size or length to determine the correct dimensions for the matrix or array.

3. Why am I getting a subscripted assignment dimension mismatch error in my MATLAB code?

A subscripted assignment dimension mismatch error occurs in MATLAB when the sizes of the values being assigned and the matrix or array being assigned to do not match. This can happen when the dimensions of the values are not compatible with the dimensions of the matrix or array, or when there is a mistake in the code that leads to mismatched dimensions.

4. How can I prevent a subscripted assignment dimension mismatch error in MATLAB?

To prevent a subscripted assignment dimension mismatch error in MATLAB, it is important to carefully check the dimensions of the values being assigned and the matrix or array being assigned to. Make sure that they are compatible and that there are no mistakes in the code that could lead to mismatched dimensions. Additionally, using functions such as size or length to determine the dimensions before assigning values can help prevent this error.

5. Is there a way to ignore a subscripted assignment dimension mismatch error in MATLAB?

No, there is not a way to ignore a subscripted assignment dimension mismatch error in MATLAB. This error indicates that there is an issue with the dimensions of the values being assigned and the matrix or array being assigned to, and ignoring it could lead to incorrect results or unexpected behavior in your code. It is important to address and fix the error rather than ignoring it.

Back
Top