Is there any way of doing this operation without a loop?

1 view (last 30 days)
I want to do following operarion between an array (A) and a vector (B):
A(i,j)/B(i)
Is there any way to do it without looping? rdivide requires both A and B to be the same size...
Thank you!
  2 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 3 Dec 2014
Edited: Azzi Abdelmalek on 3 Dec 2014
Agustin, click "Accept" next to an Answer that solves your problem.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 3 Dec 2014
Use bsxfun. Note that the non-singleton dimensions have to be the same, so you may have to transpose your matrix, but the result should be the same.
Example:
A = randi(10, 5, 4);
B = randi(20, 5, 1);
C = bsxfun(@rdivide, A, B);

More Answers (1)

Stephen23
Stephen23 on 3 Dec 2014
Edited: Stephen23 on 3 Dec 2014
Yes, use bsxfun, this is exactly what it is for:
bsxfun(@rdivide,A,B)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!