I have $2$ matrices of different sizes: $8 \times 3$ matrix $A$ and $2 \times 3$ matrix $B$. I need to subtract from each row of $B$, a particular row of $A$ and sum these values. This I need to do for all the rows for $A$ and store the result in another matrix $C$. Is there a way to do this avoiding both for loops?
I can avoid one using bsxfun but seems like I need to use one for loop for iterating over the elements of $A$.
If I understand correctly, you want to (1) subtract every row of
Afrom every row ofB, and then (2) sum all values within each resulting row.For step (1) you can use a single
bsxfun, but you need topermutedimensions:For any
m,n,subtractedRows(m,:,n)givesB(m,:)-A(n,:).For step (2) you only need to sum along second dimension: