Error using sum Dimension argument must be a positive integer scalar within indexing range.

Hello, I am trying to do this calculation and despite the error message I am having trouble understanding the problem. I need help. Thank you from before.
N=11;
i=6;
Mrandom=round(mod(sum(rand(2^i),ones(2^N-i))),1)
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

Answers (1)

2^N-i is 2^11-6 = 2048 - 6 = 2042. ones(2042) is a 2D array that is 2042 x 2042 that is all ones. You are passing that 2042 by 2042 vector as the second element of sum(), into a position where sum() expects the list (unique) of dimension numbers to sum over. But a 2042 x 2042 array is not a vector, and the 1's in that array are not unique.
Note: rand(2^i) would be rand(2^6) which would ask for a 64 x 64 array of random numbers. That is not going to be compatible in size with the 2042 x 2042 array, so we can see that you are not asking to add the ones() and the rand() result.
Are you possibly trying to take randi([0 2^i-1], 2^N-i, 2^N-i) ?? But if so then the mod() would make the remainders all zero...

2 Comments

Hello, thank you for your information. But I'm trying to code this formula here from an article on matlab and I'm not that excellent on matlab. thanking you in advance
That explanation confuses counting of random matrices with the actual random matrices, and is unclear what values are possible for the random matrices. If it wants to talk about iterating over all possible distinct 2^i by 2^i binary matrices then it should just do so instead of pretending that they are random.

Sign in to comment.

Asked:

on 21 Feb 2023

Commented:

on 22 Feb 2023

Community Treasure Hunt

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

Start Hunting!