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
7
votes
5 answers

bsxfun implementation in matrix multiplication

As always trying to learn more from you, I was hoping I could receive some help with the following code. I need to accomplish the following: 1) I have a vector: x = [1 2 3 4 5 6 7 8 9 10 11 12] 2) and a matrix: A =[11 14 1 5 8 18 …
7
votes
1 answer

How to vectorize the intersection kernel function in MATLAB?

I need to pre-compute the histogram intersection kernel matrices for using LIBSVM in MATLAB. Assume x, y are two vectors. The kernel function is K(x, y) = sum(min(x, y)). In order to be efficient, the best practice in most cases is to vectorize the…
Peiyun
  • 171
  • 1
  • 2
  • 13
6
votes
4 answers

Creating and manipulating three dimensional matrices in Matlab

I'm desperately trying to avoid a for loop in Matlab, but I cannot figure out how to do it. Here's the situation: I have two m x n matrices A and B and two vectors v and w of length d. I want to outer multiply A and v so that I get an m x n x d…
PengOne
  • 48,188
  • 17
  • 130
  • 149
6
votes
3 answers

How to sum parts of a matrix of different sizes, without using for loops?

I have a relatively large matrix NxN (N~20,000) and a Nx1 vector identifying the indices that must be grouped together. I want to sum together parts of the matrix, which in principle can have a different number of elements and non-adjacent…
user9998992
  • 124
  • 8
6
votes
1 answer

BSXFUN on memory efficiency with relational operations

There are mainly two things I would like to research on about here - There are six built-in relational operations for use with bsxfun : @eq (equal), @ne (not-equal), @lt (less-than), @le (less-than or equal), @gt (greater-than) and @ge…
Divakar
  • 218,885
  • 19
  • 262
  • 358
6
votes
1 answer

extension of bsxfun to cater to particular indices in a matrix

I have a problem. I am trying to implement this short piece of code. The basic steps I have already done. Please check my code below: clc;clear all;close all; A=round(-3+(6).*rand(5,5)); B=round(-3+(6).*rand(5,5)); %//The check matrix stores the…
roni
  • 1,443
  • 3
  • 28
  • 49
5
votes
1 answer

Least squares circle fitting using MATLAB Optimization Toolbox

I am trying to implement least squares circle fitting following this paper (sorry I can't publish it). The paper states, that we could fit a circle, by calculating the geometric error as the euclidean distance (Xi'') between a specific point (Xi)…
WebMonster
  • 2,981
  • 2
  • 22
  • 29
5
votes
3 answers

bsxfun-like for matrix product

I need to multiply a matrix A with n matrices, and get n matrices back. For example, multiply a 2x2 matrix with 3 2x2 matrices stacked as a 2x2x3 Matlab array. bsxfun is what I usually use for such situations, but it only applies for element-wise…
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
5
votes
2 answers

apply bsxfun or arrayfun to every row of a matrix

There are two matrices, A and B with size m-by-4 and n-by-4 respectively. My question is how to apply a function f, which takes two 1x4 vectors as input, on every row of A and B. The result will be a matrix with size mxn. The element [i, j] in…
Fihop
  • 3,127
  • 9
  • 42
  • 65
5
votes
4 answers

Using `bsxfun` for non-numeric data

Is there an equivalent to bsxfun for non-numeric data? For example, I want to compare all pairs of strings stored in two cell-arrays: >> a = {'aa', 'bb', 'cc'}; >> b = {'dd', 'aa'}; >> bsxfun( @strcmp, a, b' ); % not working for cells :-(
Shai
  • 111,146
  • 38
  • 238
  • 371
4
votes
1 answer

Is bsxfun still optimal in MATLAB?

I did bump into this question while searching for this topic, but this one seems to be outdated. Reading https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b , implicit expansion was introduced in 2016b, but I can still…
Jang
  • 43
  • 2
4
votes
3 answers

How can I average a matrix every nth elements in a vectorized way?

In MATLAB, given a 36 x 17 matrix A, I want to average every 6th elements of each column, creating a 6 x 17 matrix B. I can achieve it using the following code: A = rand(36, 17); B = [mean(A(1:6:36,:)); mean(A(2:6:36,:)); mean(A(3:6:36,:));…
AJMA
  • 1,134
  • 2
  • 13
  • 28
4
votes
3 answers

Accelerate Matlab nested for loop with bsxfun

I have a graph n x n graph W described as its adjacency matrix and a n vector of group labels (integers) of every node. I need to count the number of links (edges) between nodes in group c and nodes in group d for every pair of groups. Do to this I…
linello
  • 8,451
  • 18
  • 63
  • 109
4
votes
1 answer

Use of bsxfun with singleton expansion with matrixes of three dimensions

I'm using bsxfun to vectorize an operation with singleton expansion between matrixes of sizes: MS: (nms, nls) KS: (nks, nls) The operation is the sum of the absolute differences between each value MS(m,l) with m in 1:nms and l in 1:nls, and every…
jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
4
votes
1 answer

Vectorizing the assignment of values into a 3D array

Here is the simple code: A=zeros(60,60,30); a=rand(28,28,30); for i=1:30 m=round(rand*32)+1; %because 60-28=32 n=round(rand*32)+1; A(m:m+27,n:n+27,i)=a(:,:,i); end What it does is simply take a 28*28 random matrix and "plant" it inside…
alonhzn
  • 150
  • 1
  • 7
1
2
3
11 12