Inputs must be 2-D, or at least one input must be scalar
25 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Guillaume
2016-7-28
编辑:Guillaume
2016-7-28
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 个评论
Guillaume
2016-7-29
编辑:Guillaume
2016-7-29
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!