Analysis of Bsxfun and unique functions in MATLAB

Source: Internet
Author: User

I. Source of the problem

comes from a copy of the LSH code, recorded.

two. function parsing2.1 Bsxfun

Bsxfun is a matlab from version r2007a to provide a function, the function is "applies an element-by-element binary operation to arrays A and B, with Singleton exp Ansion enabled.

function is used in two elements of an array are computed one after the other. For example , when we want to perform certain operations on each column of a matrix A or on each row of a vector a of equal length (compare size, multiplication, etc.), we can only use the loop method or the vector a that the Repmat function will operate to copy the matrix of the same size sing Woo A, and then operate. From the beginning of Matlab r2007a, when encountering similar problems, we have a concise and efficient method, that is, the use of Bsxfun function.

2.2 Unique

Format B = Unique (a)% takes a vector of the non-repeating elements of set a.
b = Unique (A, ' rows ')% returns a, B matrix consisting of different line elements.
[B,i,j] = unique (...)%i reflects the position of elements in B in the original vector (matrix a); J reflects the position of the original vector (matrix a) in B

Reference: http://blog.sina.com.cn/s/blog_5efed5800100crs2.html

three. Example Analysis3.1 Bsxfun  

Let me give you an example. Suppose we have a list of vectors and a line of vectors. A = Randn (3,1), B = Randn (1,3), we can easily use the external multiplication of matlab c=a*b to come, but if we want to use "plus"? In other words, the multiplication sign in the solution process for the plus-sign?
At this point we can use C=bsxfun (@plus, A, b) to achieve.
Bsxfun execution is like this, if A and B are the same size, then c=a+b. But if there is a dimension different, and a or B must have a dimension of 1 in this dimension, then the Bsxfun will be less of this virtual copy of the same as the number of dimensions. In our case, the first dimension of B is only 1 (one row), so bsxfun copies B 3 times to form a 3x3 matrix, and also copies a into a 3x3 matrix. This is equivalent to C=repmat (a,1,3) +repmat (b,3,1).

here Repmat is an explicit copy, which of course brings memory consumption. While Bsxfun is a virtual copy, it is actually implemented by for, which is equivalent to for (I=1:3), for (J=1:3), C (i,j) =a (i) +b (j); End,end. However, Bsxfun will not have additional time for the use of MATLAB. The first two implementations are similar in terms of computational time, much higher than the for implementation. However, if the data is large, the second implementation may have a memory problem. So bsxfun the best.

Here @plus is the function of the addition of the number of handles, the corresponding subtraction @minus, multiplication @times, and so on, the specific visible doc Bsxfun. Can also be M files.

@plus@minus@times@rdivide@ldivide@power@max@min@rem@mod@atan2@hypot@eq@ne@lt< EM id= "__mcedel" > @le < EM id= "__mcedel" > @gt < EM id= "__mcedel" > @ge < EM id= "__mcedel" > @and < EM id= "__mcedel" > @or @xor

Reference http://blog.sina.com.cn/s/blog_9e67285801010ttn.html

3.2rempat

>> a=[1 1 2 2 4 4 6 4 6]
A =
1 1 2 2 4 4 6 4 6
>> [C,i,j]=unique (A)
c =
1 2 4 6
i =
2 4 8 9%i reflects the position of the elements in B in the original vector (matrix A);
j =
1 1 2 2 3 3 4 3 4%j reflects the position of the original vector (matrix a) in B

Example 1-40
>> a=[1 2 2 4;1 1 4 6;1 1 4 6]
A =
1 2 2 4
1 1 4 6
1 1 4 6
>> [C,i,j]=unique (A, ' rows ')
C =
1 1 4 6
1 2 2 4
i =
3
1
j =
2
1
1  

How do you expand each row or column element of a matrix by a different multiple? such as [1 2 3;4 5 6; 7 8 9], the first column element is multiplied by 1, the second column element is 2, and the third column element is multiplied by 4.

using the Bsxfun function, you can give the following code: a = [1,2,3;4,5,6;7,8,9]; Acol = Bsxfun (@times, a,[1 2 4])

Analysis of Bsxfun and unique functions in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.