bsxfun

Find the distance from one point in a matrix to all other points in a matrix

白昼怎懂夜的黑 提交于 2019-11-27 15:25:59
I have a matrix a and I want to calculate the distance from one point to all other points . So really the outcome matrix should have a zero (at the point I have chosen) and should appear as some sort of circle of numbers around that specific point. This is what I have already but I cant seem to get the correct outcome. a = [1 2 3 4 5 6 7 8 9 10] for i = 2:20 a(i,:) = a(i-1,:) + 1; end N = 10 for I = 1:N for J = 1:N dx = a(I,1)-a(J,1); dy = a(I,2)-a(J,2); distance(I,J) = sqrt(dx^2 + dy^2) end end chappjc Your a matrix is a 1D vector and is incompatible with the nested loop, which computes

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

≯℡__Kan透↙ 提交于 2019-11-27 08:25:15
问题 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(:

pdist2 equivalent in MATLAB version 7

情到浓时终转凉″ 提交于 2019-11-26 23:04:53
I need to calculate the euclidean distance between 2 matrices in matlab. Currently I am using bsxfun and calculating the distance as below( i am attaching a snippet of the code ): for i=1:4754 test_data=fea_test(i,:); d=sqrt(sum(bsxfun(@minus, test_data, fea_train).^2, 2)); end Size of fea_test is 4754x1024 and fea_train is 6800x1024 , using his for loop is causing the execution of the for to take approximately 12 minutes which I think is too high. Is there a way to calculate the euclidean distance between both the matrices faster? I was told that by removing unnecessary for loops I can reduce

bsxfun implementation in matrix multiplication

喜你入骨 提交于 2019-11-26 23:02:05
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 10 8 19 13 20 16] I need to be able to multiply each value from x with every value of A , this means: new_matrix = [1* A 2* A 3* A ... 12* A] This will give me this new_matrix of size (12*m x n) assuming A (mxn) . And in this case (12*4x3) How can I do this using bsxfun from matlab? and, would this method be faster than a for-loop ? Regarding my for-loop , I need some

Powers of a matrix

丶灬走出姿态 提交于 2019-11-26 21:58:18
问题 I have a square matrix A (nxn). I would like to create a series of k powers of this matrix into an nxnxk multidimensional matrix (Not element-wise but actual powers of the matrix), i.e.getting [A^0 A^1 A^2..A^k] . It's sort of a varied vandermonde for matrix case. I am able to do it with loops but it is annoying and slow. I tried using bsxfun but no luck since I am probably missing something here. Here is a simple loop that I did: for j=1:1:100 final(:,:,j)=A^(j-1); end 回答1: You are trying to

Find the distance from one point in a matrix to all other points in a matrix

≯℡__Kan透↙ 提交于 2019-11-26 17:09:13
问题 I have a matrix a and I want to calculate the distance from one point to all other points . So really the outcome matrix should have a zero (at the point I have chosen) and should appear as some sort of circle of numbers around that specific point. This is what I have already but I cant seem to get the correct outcome. a = [1 2 3 4 5 6 7 8 9 10] for i = 2:20 a(i,:) = a(i-1,:) + 1; end N = 10 for I = 1:N for J = 1:N dx = a(I,1)-a(J,1); dy = a(I,2)-a(J,2); distance(I,J) = sqrt(dx^2 + dy^2) end

BSXFUN on memory efficiency with relational operations

ε祈祈猫儿з 提交于 2019-11-26 16:55:58
问题 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 (greater-than or equal) . Lots of times we use them on floating point numbers and being relational operations, they output logical arrays. So, it got me curious, if the inherent expansion with bsxfun when using these relational operations on floating point

In Matlab, when is it optimal to use bsxfun?

ぐ巨炮叔叔 提交于 2019-11-26 15:40:55
My Question: I've noticed that a lot of good answers to Matlab questions on SO frequently use the function bsxfun . Why? Motivation: In the Matlab documentation for bsxfun , the following example is provided: A = magic(5); A = bsxfun(@minus, A, mean(A)) Of course we could do the same operation using: A = A - (ones(size(A, 1), 1) * mean(A)); And in fact a simple speed test demonstrates the second method is about 20% faster. So why use the first method? I'm guessing there are some circumstances where using bsxfun will be much faster than the "manual" approach. I'd be really interested in seeing

Sort a matrix with another matrix

限于喜欢 提交于 2019-11-26 15:24:19
Suppose I have a matrix A and I sort the rows of this matrix. How do I replicate the same ordering on a matrix B (same size of course)? E.g. A = rand(3,4); [val ind] = sort(A,2); B = rand(3,4); %// Reorder the elements of B according to the reordering of A This is the best I've come up with m = size(A,1); B = B(bsxfun(@plus,(ind-1)*m,(1:m)')); Out of curiosity, any alternatives? Update: Jonas' excellent solution profiled on 2008a (XP): n = n 0.048524 1.4632 1.4791 1.195 1.0662 1.108 1.0082 0.96335 0.93155 0.90532 0.88976 n = 2m 0.63202 1.3029 1.1112 1.0501 0.94703 0.92847 0.90411 0.8849 0.8667

pdist2 equivalent in MATLAB version 7

穿精又带淫゛_ 提交于 2019-11-26 09:14:00
问题 I need to calculate the euclidean distance between 2 matrices in matlab. Currently I am using bsxfun and calculating the distance as below( i am attaching a snippet of the code ): for i=1:4754 test_data=fea_test(i,:); d=sqrt(sum(bsxfun(@minus, test_data, fea_train).^2, 2)); end Size of fea_test is 4754x1024 and fea_train is 6800x1024 , using his for loop is causing the execution of the for to take approximately 12 minutes which I think is too high. Is there a way to calculate the euclidean