Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
division by columns

Subject: division by columns

From: Wendy

Date: 16 Mar, 2010 18:43:09

Message: 1 of 7

Hi all,

I have two matrices

A = [4 3 1;
       8 2 3;
       9 6 2]

B = [1 2;
       3 1;
       6 5];

my code is
counter = 0;
for i = 1:size(A,2);
for j = 1:size(B,2);
     counter = counter + 1;
     output(:,counter) = A(:,i)./B(:,j);
end
end

Instead of going through the loop, is there a function that I can calculate the division of between every column of A and every column of B at once.

Thank you,
Wendy

Subject: division by columns

From: us

Date: 16 Mar, 2010 19:06:02

Message: 2 of 7

"Wendy " <wlq121@gmail.com> wrote in message <hnojft$svo$1@fred.mathworks.com>...
> Hi all,
>
> I have two matrices
>
> A = [4 3 1;
> 8 2 3;
> 9 6 2]
>
> B = [1 2;
> 3 1;
> 6 5];
>
> my code is
> counter = 0;
> for i = 1:size(A,2);
> for j = 1:size(B,2);
> counter = counter + 1;
> output(:,counter) = A(:,i)./B(:,j);
> end
> end
>
> Instead of going through the loop, is there a function that I can calculate the division of between every column of A and every column of B at once.
>
> Thank you,
> Wendy

1) you loop is probably as fast as it gets...
    you should, however, preallocate your OUTPUT mat (see below)...

2)
one of the other possible solutions

     a=[4,3,1; 8,2,3; 9,6,2];
     b=[1,2; 3,1; 6,5];
     r=nan(size(a,1),size(a,2)*size(b,2)); % <- preallocation...
for i=1:size(b,2)
     r(:,i:2:end)=bsxfun(@rdivide,a,b(:,i));
end
     disp(r);
%{
       4 2 3 1.5 1 0.5
 2.66667 8 0.666667 2 1 3
     1.5 1.8 1 1.2 0.333333 0.4
%}

us

Subject: division by columns

From: David Young

Date: 16 Mar, 2010 19:26:22

Message: 3 of 7

This does it - but it's a touch messy with all the transposes, and someone may well come up with a neater way. Also, if your real A and B are big, it uses a lot of memory, but that's probably necessary if you are going to vectorise the code. Anyway:

% Assemble the columns of A into a single vector
Ap = A';
Avec = Ap(:);
% Copy this the number of times needed for the inner loop of the original code
Arepvec = repmat(Avec, 1, size(B,2));
% Reshape this into a matrix, so that there are multiple adjacent copies of each column
Arep = (reshape(Arepvec', size(A,2)*size(B,2), size(A,1)))';
% Copy B the number of times needed for the outer loop of the original code
Brep = repmat(B, 1, size(A,2));
% And finally, do the division on all combinations of columns
output = Arep ./ Brep;

Subject: division by columns

From: Bruno Luong

Date: 16 Mar, 2010 19:49:21

Message: 4 of 7

[m na] = size(A);
output = bsxfun(@rdivide, reshape(A, [m 1 na]), B)
output = reshape(output, m, [])

Bruno

Subject: division by columns

From: Matt Fig

Date: 16 Mar, 2010 19:52:26

Message: 5 of 7

Yet another:

cA = size(A,2); % assumes size(A,1)==size(B,1)
cB = size(B,2);
x = 1:cA;
y = (1:cB).';
output = A(:,x(ones(1:cB),:))./B(:,y(:,ones(1,cA)));

Subject: division by columns

From: us

Date: 16 Mar, 2010 20:28:24

Message: 6 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <hnonhq$2vh$1@fred.mathworks.com>...
> Yet another:

the usual: the timing...

bottom line
- for all mats: loser US
- for small mats: winner MF
- for large mats: winner BL

us

% wintel system: ic2/2*2.6ghz/2gb/winxp.sp3.32/r2010a.32
%{
% run 1: small size
     0.041597 % <- us
     0.02161 % <- bruno luong
     0.01754 % <- matt fig
% run 2: large size
     3.8307
     2.0062
     5.2128
%}

     nt=1000; % <= #trials
for k=1:2
if k == 1
     A=rand(3,3);
     B=rand(3,2);
else
     A=rand(300,300);
     B=rand(300,2);
end
% us
     tic;
for j=1:nt
     r1=nan(size(A,1),size(A,2)*size(B,2)); % <- preallocation: once only...
for i=1:size(B,2)
     r1(:,i:2:end)=bsxfun(@rdivide,A,B(:,i));
end
end
     t1=toc;
% bruno luong
     tic;
for j=1:nt
     [m na]=size(A);
     r2=bsxfun(@rdivide,reshape(A, [m,1,na]),B);
     r2=reshape(r2,m,[]);
end
     t2=toc;
% matt fig
     tic;
for j=1:nt
     cA=size(A,2); % assumes size(A,1)==size(B,1)
     cB=size(B,2);
     x=1:cA;
     y=(1:cB).';
     r3=A(:,x(ones(1:cB),:))./B(:,y(:,ones(1,cA)));
end
     t3=toc;

     disp([t1;t2;t3]);
     isequal(r1,r2,r3)
% ans = 1 % <- both runs...
end

Subject: division by columns

From: Matt Fig

Date: 16 Mar, 2010 21:20:22

Message: 7 of 7

"us " <us@neurol.unizh.ch> wrote in message <hnopl8$69n$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <hnonhq$2vh$1@fred.mathworks.com>...
> > Yet another:
>
> the usual: the timing...
>
> bottom line
> - for all mats: loser US
> - for small mats: winner MF
> - for large mats: winner BL

I'd be happy to be beaten by either of you guys any day ;-).

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us