Why do I receive this message errorr ?
1 view (last 30 days)
Show older comments
RADWAN A F ZEYADI
on 8 Oct 2021
Commented: Walter Roberson
on 10 Oct 2021
Array dimensions must match for binary array op
this is the message error that i have obtained when i tried to perturb my data by this equation( Pert = obs + sqrt(alpha*Cd).*randn(nd, ne))
where obs=51*71*3 .,, alpha =0.25,, cd=71*71 ,,nd=51,ne=500 .
why do i recive this error ? thanks in advance
6 Comments
Walter Roberson
on 10 Oct 2021
No. It is simply impossible to do what you want. You are trying to take a 51 x 71 x 3 array and add something to it to get a 51 x 500 x 3 result.
pagemtimes( obs, sqrt(alpha*Cd) * randn(71,500))
would get you a 51 x 500 x 3 result -- but it is not clear that is the formula that you would want, as it is pure multiplication with no addition. Also, potentially you might be wanting
pagemtimes( obs, pagemtimes(sqrt(alpha*Cd), randn(71,500,3)) )
Accepted Answer
Walter Roberson
on 8 Oct 2021
Your obs is 51 x 71 x 3. You are adding something to it. In order for the addition to work, the size of what is being added must agree with the size of obs for any dimension that is not length 1 -- dimension length 1 will be automatically replicated as needed. For example if what you were adding was 51 x 71 x 1 then that would work because the code would be considered to be equivalent to obs + repmat(expression, 1, 1, size(obs,3))
So what are we adding? We are adding the result of a multiplication.
When you use the .* operator, the size of what is being multiplied must agree with the size of obs for any dimension that is not length 1 -- dimension length 1 will be automatically replicated as needed.
The first item you are multiplying is 71 x 71 . The second item you are multiplying is 51 x 500 . These two items are not the same in any dimension, so .* element-by-element multiplication cannot proceed.
Let us hypothesize for a moment that you should have been using the * matrix-multiplication operator instead of the .* element-by-element operation. For A*B to work, size(A,2) must be the same as size(B,1) . But the second dimension of 71 x 71 is not the same as the first dimension of 51 x 500
How could we rescue this? Well.... suppose we did
Pert = obs + randn(nd, ne) * sqrt(alpha*Cd)
where sqrt(alpha*Cd) was size 500 x 71. In that case, the randn(51,500) matrix-multiply by 500 x 71 would be acceptable to create a 51 x 71 matrix, and then the 51 x 71 x 3 on the left could add to the 51 x 71 on the right because the mismatch dimension (3 on the left, 1 on the right) has a 1 for one of them so replication would take place.
But perhaps there is no effective choice in the size of Cd, that it must be 71 x 71: how would we rescue the situation then? Well if we made ne = 71 then
Pert = obs + randn(nd, ne) * sqrt(alpha*Cd)
would have (51 x 71) * (71 x 71) which would give a 51 x 71 result on the right, which could be added to the 51 x 71 x 3 on the left.
So... what would the 500 be about then? That is a bit hard to guess without more context. Possibly you are wanting to do this whole setup 500 times -- but if so, what size of output are you hoping for? 51 x 71 x 3 x 500 ? Algebraic matrix multiplication is not defined for 3 dimensions, but depending on the mathematics that is desired, sometimes functions such as pagemtimes() https://www.mathworks.com/help/matlab/ref/pagemtimes.html can be used.
0 Comments
More Answers (0)
See Also
Categories
Find more on Financial Toolbox 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!