Subtract raws of a matrix from an array

2 views (last 30 days)
luca
luca on 10 Sep 2019
Edited: Stephen23 on 10 Sep 2019
Given the following array
Q= [42 42 42 42 42 42 ]
and a matrix M
M= [33 38 31 37 35 37 ;
36 33 33 33 31 35 ;
31 31 27 35 30 38 ;
37 28 36 36 30 33 ;
34 30 34 30 30 33 ;
32 27 29 34 37 29 ;
36 33 35 31 33 34 ;
39 29 28 35 32 37 ;
34 28 36 32 35 31 ;
37 36 25 33 35 34 ;
36 36 31 37 33 37 ;
32 32 32 33 36 35 ;
36 36 31 33 33 37 ;
33 35 30 31 32 32 ;
33 30 35 38 34 34 ;
33 35 34 32 32 32 ]
I want to obtain a matrix C with the difference between the raw of Q and each raw of M, obtaining
C= [9 4 11 5 7 5;
6 9 9 9 11 7;
...]
Does someone can help me?

Accepted Answer

Stephen23
Stephen23 on 10 Sep 2019
Edited: Stephen23 on 10 Sep 2019
MATLAB versions prior to R2016b:
>> C = bsxfun(@minus,Q,M)
C =
9 4 11 5 7 5
6 9 9 9 11 7
11 11 15 7 12 4
5 14 6 6 12 9
8 12 8 12 12 9
10 15 13 8 5 13
6 9 7 11 9 8
3 13 14 7 10 5
8 14 6 10 7 11
5 6 17 9 7 8
6 6 11 5 9 5
10 10 10 9 6 7
6 6 11 9 9 5
9 7 12 11 10 10
9 12 7 4 8 8
9 7 8 10 10 10
MATLAB versions R2016b or later:
>> Q-M
ans =
9 4 11 5 7 5
6 9 9 9 11 7
11 11 15 7 12 4
5 14 6 6 12 9
8 12 8 12 12 9
10 15 13 8 5 13
6 9 7 11 9 8
3 13 14 7 10 5
8 14 6 10 7 11
5 6 17 9 7 8
6 6 11 5 9 5
10 10 10 9 6 7
6 6 11 9 9 5
9 7 12 11 10 10
9 12 7 4 8 8
9 7 8 10 10 10

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by