Questions tagged [bsxfun]

A versatile and useful Matlab command for applying element-by-element binary operation to two arrays with singleton expansion enabled.

bsxfun (binary singleton expansion function) is a built-in Matlab function that allows for applying an element-by-element binary operation to two arrays with singleton expansion enabled. It can be used to avoid unnecessary and costly calls to repmat, and to replace specific for-loop structures.

See the current documentation for bsxfun. The function debuted in Matlab 7.4 (R2007a). Those with versions of Matlab prior to this can try this bsxfun substitute on the MathWorks File Exchange.


What can be done with bsxfun?

The following examples demonstrate all sorts of useful and efficient uses of bsxfun.

Normalizing vectors

Suppose you have a collection of n feature vectors with dimension d in a 2D matrix feat of size n-by-d. You wish to normalize all the features to have Euclidean norm of 1.

Here's how it can be done with bsxfun:

>> l2norm = sqrt( sum( feat.^2, 2 ) ); % n-by-1 vector of the norms of the feature vectors
>> normFeat = bsxfun( @rdivide, feat, l2norm ); % ta-da!

Subtracting vectors

Suppose you have n d dimensional vectors and you wish to find their distance from some predefined location c.

Here's how it can be done with bsxfun:

>> dst = sqrt( sum( bsxfun( @minus, vectors, c ).^2, 2 ) );

Creating a logical triangular matrix

What if you have a matrix A of n-by-n and you want a logical matrix L that selects the lower triangle of A (that is L(ii,jj) is true iff ii > jj)?

Here's how it can be done with bsxfun:

>> L = bsxfun( @gt, (1:n)', 1:n );

Computing the outer-product of many vectors simultaneously

Suppose you have two collections of n factors of d dimension U and V (both of size n-by-d), and you want to compute the outer-product O(:,:,ii) = U(ii,:)'*V(ii,:) for all the n vector-pairs.

Here's how it can be done with bsxfun:

>> O = bsxfun( @times, permute( U, [3 2 1] ), permute( V, [2 3 1] ) );
168 questions
0
votes
1 answer

count match value two matrix using bsxfun

I use c=bsxfun(@eq,b,a) to compare value of two matrix. but I find it difficult to count un-match values. for example, I use this code a = [1 2 3 4 7 6; ... 3 2 4 6 7 2 ]; b = [1 3 2 4 5 7; ... 3 4 5 6 7 2; ... 2 3 4 5 6 6]; for i =…
0
votes
1 answer

Matlab - Matrix Division by Zero - Zeros and NaNs

I am trying to convert a precision matrix sigmaT to a covariance matrix. I've tried two approaches: covMat = zeros(size(sigmaT)); for i=1:t covMat(:, :, i) = eye(D)/sigmaT(:,:,i); end and covMat = bsxfun(@rdivide, eye(D), sigmaT); Some of the…
davhab
  • 805
  • 2
  • 9
  • 17
0
votes
2 answers

MATLAB - bsxfun for negative values

I am normalizing a data set using the command X=bsxfun(@times,bsxfun(@minus,X,min(X,[],1)),1./max(X,[],1)) I tried this function on two different data sets . One had negative values as well . The others didn't . The data set with no negative…
Sahil Thapar
  • 301
  • 1
  • 3
  • 18
0
votes
1 answer

bsxfun rows subtraction in loop

I have a text file D with 98 rows and 2 columns. looks like this: 10 0.261344 11 0.456167 12 0.668595 2 0.481754 ... etc I have another excel file M with 17 rows and 2 col. I want to subtract all rows values…
mil
  • 301
  • 1
  • 2
  • 15
0
votes
1 answer

How to vectorize the distances between two matrices?

I'm building a K-nearest neighbors classifier, and I'd like to get my distance calculations done all at once (it would help too, as the unvectorized version is taking a loong time to run). I have a test dataset of size 28000 examples x 784 features,…
user1956609
  • 2,132
  • 5
  • 27
  • 43
0
votes
2 answers

Translating a line of Matlab (bsxfun, rdivide) to Python

I am translating a Matlab function to Python. Unfortunately I am not a Matlab expert and it is hard for me to understand some lines, e. g. this one: a = [[0, 1]; [2, 3]] bsxfun(@rdivide, sqrt(a), a) I did not really understand it yet, but I think…
alfa
  • 3,058
  • 3
  • 25
  • 36
0
votes
1 answer

matlab: cellfun logic

I have a (pretty complicated - for me anyway) cellfun operation and I need a wise head to cast their eye over it to tell if is actually doing what I have intended: b = cellfun(@(x) nansum(bsxfun(@times,…
brucezepplin
  • 9,202
  • 26
  • 76
  • 129
0
votes
1 answer

matlab: multiplication inside cellfun

Hi I have the following cellfun operation: b = cellfun(@(x) nansum(cross(u{1},x)),r,'UniformOutput',false); where u and r are vectors. I want to multiply the result of the cross product by a scalar called I, but it has to inside the summation.…
brucezepplin
  • 9,202
  • 26
  • 76
  • 129
0
votes
2 answers

matlab: understanding matlab behavior

Could somebody explain the following code snippet? I have no background in computer science or programming and just recently became aware of Matlab. I understand the preallocation part from data=ceil(rand(7,5)*10)... to ...N*(N-1)/2). I need to…
Buntalan
  • 183
  • 1
  • 3
  • 14
0
votes
1 answer

Matlab optimisation

Apologies if this is too much like a 'do my homework' question. I am a bit stuck and this is my go to place. This is not code I wrote, but I am now maintaining it. I have a tool that runs for around 8 hours on a bad day. I profiled it today. …
N t
  • 271
  • 3
  • 15
0
votes
2 answers

matlab matrices of different sizes (indexing, for loop, and bsxfun)

I have two matrices of different sizes. Let's just define matrix {a} as a(1:10) <10 x 1> and matrix {b} as b(6:10) <5 x 1>. I need a for loop or equivalent (bsxfun) which gets the difference between matrix {a} and {b}, the code will iterate based on…
Buntalan
  • 183
  • 1
  • 3
  • 14
-1
votes
1 answer

How to convert a nested loop into parfor loop

This is my from my MATLAB script. function [ Im ] = findBorders( I ) Im = false(size(I)); I = padarray(I, [1, 1], 1); [h w] = size(Im); bkgFound = false; for row = 1 : h for col = 1 : w if I(row + 1, col + 1) bkgFound =…
-1
votes
1 answer

How to avoid loops by Vectorizing below code?

The code below is correct, but I want to vectorize it (and may convert to GPU) to increase the speed. How can I convert it to vector form? RF = 4; inhibatory = 0; overlap=3; act_funct = 'sig'; gap = RF-overlap; Image1 = rand(30,22); …
khan
  • 531
  • 6
  • 29
-1
votes
1 answer

Efficient matrix multiplications in Matlab

What's the best way to do the following (in Matlab) if I have two matrices A and B, let'say both of size m-by-n: C = zeros(m,m); for t=1:n C=C+A(:,t)*B(:,t)'; end
-1
votes
1 answer

Multiply matrices layer by layer

I want to do this without loops: % A ~ 4x2x3; B ~ 4x3x2; C ~ 4x2x2; for i=1:4 C(i,:,:) = squeeze(A(i,:,:))*squeeze(B(i,:,:)); end Thanks!
Kuraga
  • 331
  • 3
  • 16
1 2 3
11
12