Subtracting Row and column vectors
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Akira Agata
le 13 Août 2017
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'.
Réponses (2)
Stephen23
le 13 Août 2017
Modifié(e) : Stephen23
le 13 Août 2017
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 commentaires
Akira Agata
le 17 Août 2017
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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!