Error using sum Dimension argument must be a positive integer scalar within indexing range.
Show older comments
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)
Answers (1)
Walter Roberson
on 21 Feb 2023
1 vote
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
Willy Sean
on 22 Feb 2023
Walter Roberson
on 22 Feb 2023
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.
Categories
Find more on Logical 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!