Subtracting Row and column vectors
12 次查看(过去 30 天)
显示 更早的评论
I need to subtract vectors of different dimensions, "A" with dimension of (1x91)double and "B" with dimension of (60x1) double. In addition, there are two more vectors "C" with dimension of (1x119) double and "D" with dimension of (119x1) double.
How can I get (X=A-B) and (Y=C-D) in such cases?
2 个评论
Akira Agata
2017-8-13
What do you mean by 'X=A-B'?
X(1) = A(1) - B(1), ..., X(60) = A(60) - B(60) ?
If so, what about X(61) to X(91) ?
I need the detailed definition of your calculation rule of 'A-B' and 'C-D'.
回答(2 个)
Stephen23
2017-8-13
编辑:Stephen23
2017-8-13
One simple solution that does not require knowing anything about the sizes of the vectors:
>> A = randi(9,1,9)
A =
1 3 4 3 2 8 4 8 4
>> B = randi(9,6,1)
B =
7
4
8
7
4
2
>> C = diag(bsxfun(@minus,A,B))
ans =
-6
-1
-4
-4
-2
6
For newer MATLAB versions with implicit expansion:
diag(A-B)
Note that this is not particularly memory efficient so don't use this with large vectors!
0 个评论
Akira Agata
2017-8-17
I think the following would be one possible solution. By using zero-padding and transpose, I have adjusted A and B, C and D, to the same size.
%%Example of your data
A = rand(1,91);
B = rand(60,1);
% Adjut B to a row vector with the same length with A by zero padding
B1 = [B',zeros(1, length(A) - length(B))];
% Calculate X = A - B
X = A - B1;
%%Example of your data
C = rand(1,119);
D = rand(119,1);
% Adjust D to a row vector with the same length with C
D1 = D';
% Calculate Y = C - D
Y = C - D1;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!