bsxfun

Using `bsxfun` for non-numeric data

筅森魡賤 提交于 2019-12-01 18:17:52
问题 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 :-( 回答1: I'm afraid there's no such equivalent for cell-arrays :-( As far as I can see, you can either: Follow Oleg's suggestion and use loops Use existing implementations such as mAryCellFcn or csxfun from the File Exchange. Roll your own function. For example,

Using `bsxfun` for non-numeric data

别说谁变了你拦得住时间么 提交于 2019-12-01 18:09:34
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 :-( I'm afraid there's no such equivalent for cell-arrays :-( As far as I can see, you can either: Follow Oleg's suggestion and use loops Use existing implementations such as mAryCellFcn or csxfun from the File Exchange. Roll your own function. For example, here's a variant of Robert's idea that works for inputs of any dimensions (under the restrictions of bsxfun

apply bsxfun or arrayfun to every row of a matrix

人盡茶涼 提交于 2019-12-01 17:49:58
问题 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 m x n . The element [i, j] in result is f(A(i, :), B(j, :)) . For example: A = rand(3, 4); B = rand(5, 4); for i = 1 : 3 for j = 1 : 5 result(i, j) = rectint(A(i, :), B(j, :)); end end Can I use bsxfun or arrayfun to do this job? 回答1: You can use arrayfun , if you first use

apply bsxfun or arrayfun to every row of a matrix

狂风中的少年 提交于 2019-12-01 17:47:54
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 m x n . The element [i, j] in result is f(A(i, :), B(j, :)) . For example: A = rand(3, 4); B = rand(5, 4); for i = 1 : 3 for j = 1 : 5 result(i, j) = rectint(A(i, :), B(j, :)); end end Can I use bsxfun or arrayfun to do this job? Luis Mendo You can use arrayfun , if you first use meshgrid to generate all combinations of rows: [ii jj] = meshgrid(1:size(A,1),1:size(B,1)); result

Is there an equivalent to the MATLAB function bsxfun in python?

不问归期 提交于 2019-11-30 17:09:45
I'm trying to port some of my code from matlab to python, and some of it uses the bsxfun() function for virtual replication followed by multiplication or division (I also use it for logical operations). I'd like to be able to do this without actually replicating the vector (either with a function or with some kind of diagonal matrix) before multiplying or dividing to save on memory and time. If there's an equivalent of bsxfun in a C library of some kind, that would of course also work. There isn't really an equivalent of bsxfun, that I'm aware of, although numpy does take care of a lot of

Matlab - bsxfun no longer faster than repmat?

懵懂的女人 提交于 2019-11-29 05:30:57
I'm trying to find the fastest way of standardizing a matrix in Matlab (zero mean, unit variance columns). It all comes down to which is the quickest way of applying the same operation to all rows in a matrix. Every post I've read come to the same conclusion: use bsxfun instead of repmat . This article, written by Mathworks is an example: http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/ However, when trying this on my own computer repmat is always quicker. Here are my results using the same code as in the article: m = 1e5; n = 100; A = rand(m,n); frepmat = @

Generating arrays using bsxfun with anonymous function and for elementwise subtractions - MATLAB

天大地大妈咪最大 提交于 2019-11-28 14:08:10
I have the following code: n = 10000; s = 100; Z = rand(n, 2); x = rand(s, 1); y = rand(s, 1); fun = @(a) exp(a); In principle, the anonymous function f can have a different form. I need to create two arrays. First, I need to create an array of size n x s x s with generic elements fun(Z(i, 1) - x(j)) * fun(Z(i, 2) - y(k)) where i=1,...n while j,k=1,...,s . What I can easily do, is to construct matrices using bsxfun , e.g. bsxfun(@(x, y) fun(x - y), Z(:, 1), x'); bsxfun(@(x, y) fun(x - y), Z(:, 2), y'); But then I would need to combine them into 3D array by multiplying element-wise each column

Vectorizing nested loops in matlab using bsxfun and with GPU

巧了我就是萌 提交于 2019-11-28 10:39:07
For loops seem to be extremely slow, so I was wondering if the nested loops in the code shown next could be vectorized using bsxfun and maybe GPU could be introduced too. Code %// Paramaters i = 1; j = 3; n1 = 1500; n2 = 1500; %// Pre-allocate for output LInc(n1+n2,n1+n2)=0; %// Nested Loops - I for x = 1:n1 for y = 1:n1 num = ((n2 ^ 2) * (L1(i, i) + L2(j, j) + 1)) - (n2 * n * (L1(x,i) + L1(y,i))); LInc(x, y) = L1(x, y) + (num/denom); LInc(y, x) = LInc(x, y); end end %// Nested Loops - II for x = 1:n1 for y = 1:n2 num = (n1 * n * L1(x,i)) + (n2 * n * L2(y,j)) - ((n1 * n2 * (L1(i, i) + L2(j, j)

How much faster is implicit expansion compared with bsxfun?

有些话、适合烂在心里 提交于 2019-11-28 09:41:08
As commented by Steve Eddins , implicit expansion (introduced in Matlab R2016b) is faster than bsxfun for small array sizes, and has similar speed for large arrays: In R2016b, implicit expansion works as fast or faster than bsxfun in most cases. The best performance gains for implicit expansion are with small matrix and array sizes. For large matrix sizes, implicit expansion tends to be roughly the same speed as bsxfun . Also, the dimension along which expansion takes place may have an influence: When there is an expansion in the first dimension, the operators might not be quite as fast as

Matlab - bsxfun no longer faster than repmat?

血红的双手。 提交于 2019-11-27 17:20:00
问题 I'm trying to find the fastest way of standardizing a matrix in Matlab (zero mean, unit variance columns). It all comes down to which is the quickest way of applying the same operation to all rows in a matrix. Every post I've read come to the same conclusion: use bsxfun instead of repmat . This article, written by Mathworks is an example: http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/ However, when trying this on my own computer repmat is always quicker.