Suppose I have a matrix like:
100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60
...
I wish to divide each row by the second row (each element by the corresponding element), so I'll get:
100 100 100 100 100 100
1 1 1 1 1 1
10 10 10 10 10 10
...
Hw can I do it (without writing an explicit loop)?
Here's a couple more equivalent ways:
The best solution is the one using BSXFUN (as posted by @Itamar Katz)
You can now use array vs matrix operations.
This will do the trick :
which will output :
This will work in Octave and Matlab since R2016b.
Use
bsxfun
:The 1st argument to
bsxfun
is a handle to the function you want to apply, in this case right-division.