Inputs must be 2-D, or at least one input must be scalar
27 views (last 30 days)
Show older comments
I have the following codes:
[l,r,h3]=size(H);
maxc=max(c);
h=H*ones(h3,1);
z1=(maxc/r)*ones(r,1);
So, when I want to run the function, I get the following error:
Error using *
Inputs must be 2-D, or at least one input must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
Error in Trump (line 19)
h=H*ones(h3,1);
Thanks
3 Comments
Answers (1)
Guillaume
on 28 Jul 2016
Edited: Guillaume
on 28 Jul 2016
You have to ensure that the things you multiply are the same size or that one of them is scalar. In H * ones(h3, 1), the right hand side ( ones(h3, 1)) is h3 rows by 1 column (a column vector). Considering the previous line, you would assume that H is 3D. The two are not compatible.
You could solve that by ensuring that the matrix of ones you're creating is the same size as H (so ones(l, r, h3)), or you could use bsxfun which would expand singleton dimensions, but in the end. all you're doing is multiplying by 1, so what's the point of all these multiplications?
2 Comments
Guillaume
on 29 Jul 2016
Edited: Guillaume
on 29 Jul 2016
Please, use the {}Code button to format your code. It's much simpler than adding a blank line between each line of code (just paste the code and click the button) and results in much more readable posts.
Are you aware of what ones does? It creates a matrix of one. Multiplying something by one (no matter how many there are) does not change its value. It's completely pointless
You even have q1 = 0*ones(I, 1), 0 times 1 is zero, so q1 = zeros(I, 1) is going to be identical.
I have no idea what you're doing, but all these *ones(something, 1) are absolutely pointless.
edit: the above assumes you meant to perform element-wise multiplication (using .* instead of *). If you were actually intending matrix multiplication, then the numbers of columns of the first matrix must be equal to the number of rows of the second matrix. Multiplying by a column of 1 is still pointless, the same can be achieved with the much clearer:
result = sum(input, 2)
See Also
Categories
Find more on Loops and Conditional Statements 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!