bsxfun

Subtracting multiple vectors from each row of an array (super broadcasting)

我的梦境 提交于 2019-12-22 10:36:39
问题 I have a data set, X that is m x 2 , and three vectors stored in a matrix C = [c1'; c2'; c3'] that is 3 x 2 . I am trying to vectorize my code that finds, for each data point in X , which vector in C is closest (squared distance). I would like to subtract each vector (row) in C from each vector (row) in X , resulting in an m x 6 or 3m x 2 matrix of differences between the elements of X and the elements of C . My current implementation does this one row in X at a time: for i = 1:size(X, 1)

Using bsxfun with an anonymous function

六月ゝ 毕业季﹏ 提交于 2019-12-22 10:22:36
问题 after trying to understand the bsxfun function I have tried to implement it in a script to avoid looping. I am trying to check if each individual element in an array is contained in one matrix, returning a matrix the same size as the initial array containing 1 and 0's respectively. The anonymous function I have created is: myfunction = @(x,y) (sum(any(x == y))); x is the matrix which will contain the 'accepted values' per say. y is the input array. So far I have tried using the bsxfun

MATLAB tutorial for programmers [closed]

旧巷老猫 提交于 2019-12-20 08:01:19
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I'm getting some new students soon, who will be writing MATLAB code. They're new to MATLAB, but they have experience coding in Java and C++. I'm going to have them go through the Getting Started section of the MATLAB help. In addition, I want to give a small tutorial with the

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

半城伤御伤魂 提交于 2019-12-18 14:26:23
问题 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. 回答1:

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

ぐ巨炮叔叔 提交于 2019-12-18 14:25:21
问题 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. 回答1:

Multiply matrices layer by layer

谁都会走 提交于 2019-12-14 03:26:57
问题 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! 回答1: Haven't benchmarked this (so this is not guaranteed to be faster), but here goes: [L, ma, na] = size(A); [L, mb, nb] = size(B); AX = reshape(permute(A, [2 1 3]), [], na); BX = reshape(permute(B, [2 3 1]), mb, []); CX = reshape(permute(reshape(AX * BX, ma, L, nb, L), [1 3 2 4]), ma, nb, []); C = permute(CX(:, :, 1:L + 1:end), [3 1 2]); Note that you

Multiply all columns of one matrix by another matrix with bsxfun

≡放荡痞女 提交于 2019-12-14 02:15:22
问题 I have two matrices A and B , both of size 4x4 . multiply the first column of A with all the other columns of B with: bsxfun(@times, A(:,1),B) but what I want to do is to repeat this operation for each column of A , i.e multiply all columns of A by all columns of B . How can I do this with bsxfun (without loops or repmat )? 回答1: Yes, by permuting the dimensions of one of the matrices to make it a 4x1x4 array: permute(bsxfun(@times, A, permute(B, [1 3 2])), [1 3 2]) 回答2: Alternatively: >> n =

Transposing matrix / Trouble understanding how bsxfun works

对着背影说爱祢 提交于 2019-12-12 21:22:05
问题 This could be a weird question because Many would be wondering why to use such a complicated function like bsxfun for transposing while you have the .' operator. But, transposing isn't a problem for me. I frame my own questions and try to solve using specific functions so that i learn how the function actually works. I tried solving some examples using bsxfun and have succeeded in getting desired results. But my thought, that i have understood how this function works, changed when i tried

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

元气小坏坏 提交于 2019-12-12 04:33:12
问题 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 this line does r / a for each row r of sqrt(a) (or is it each column?) and r / sqrt(a) can usually be translated to numpy as numpy.linalg.solve(sqrt(a).T, r.T).T The problem with this is: Matlab says the result is NaN 1.00000 0.70711 0.57735 and

Error only triggers when I don't use parfor?

扶醉桌前 提交于 2019-12-12 01:26:57
问题 In the first Matlab script below when I run it as shown I get no errors what so ever and the code produces the expected results, however when I take out matlabpool open and matlabpool close as well as changing the parfor loop to a for loop, I get the following error: Running... ??? Error using ==> mldivide Matrix is singular to working precision. Error in ==> NSS_betas at 11 betas = G\data.y2.'; Error in ==> DElambda at 19 betas(:,ii) = NSS_betas(P1(:,ii),data); end Error in ==> Individual