Matrix subtraction errors using repmat

2 views (last 30 days)
WBOZ11 on 26 Sep 2018
Edited: dpb on 27 Sep 2018
I am trying to subtract a 1x1 matrix with a 10000x1. In order to have proper matrix subtraction my formula was
A=unitwt*z; % (a 1x1 matrix)
B=a; % 10000x1 matrix
AB=repmat((A,10000,1)-B).*(a 10x1 matrix)
However when doing this, I keep getting the error:
noncomformant arguments (op1 is 1x1, op2 is 10000x1).
Is there anything I am missing?

Answers (2)

OCDER on 26 Sep 2018
Why do you need to use repmat? a 1x1 matrix is a scalar, hence you can just do A-B without repmat.
A = 2; %this is a 1x1 matrix
B = rand(10) %this is a 10x10 matrix
%these will work:
A + B
A - B
A * B
B / A
The real question is, what do you expect a 10000x1 matrix times by a 10x1 matrix to yield?
AB = (A-B) .* C
(10000x1 matrix) .* (10x1 matrix) = ?????
Or did you mean
AB = (A-B) .* C.'
(10000x1 matrix) .* (1x10 matrix) = 10000x10 matrix?

dpb on 26 Sep 2018
Edited: dpb on 27 Sep 2018
  1. You can subtract/add/multiply/divide any sized array with a scalar without repmat so you're "fixing" a problem that isn't a problem to begin with ( :) ), and
  2. You've got parentheses-nesting problem in the way you wrote the arguments to repmat if you are going to do it...
will get as far as the subtraction, but then the matrix multiply will be non-conforming as you'll have [10000x1]*[10x1] and the inner dimensions must match. You could write the transpose there for the second if the result is intended to be [10000x10].
But, as pointed out at the top, just write
or otherwise solve the latter problem on the end result size.
I just noticed both OCDER and I missed the 'dot' in the .* element multiply. There's an issue there in commensurate sizes as well; the problem in answering the Q? is that the intended result is ill-defined such that the intent is not known.
Explain what the output result is intended to be; perhaps illustrate the desired result with a set of small sample inputs (making up such an example might just let you see the answer to the problem on your own, even).


Find more on Creating and Concatenating Matrices 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!