Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: element-wise operators, and bsxfun
Replies: 5   Last Post: May 17, 2014 9:06 AM

 Messages: [ Previous | Next ]
 Niko Posts: 6 Registered: 1/29/14
element-wise operators, and bsxfun
Posted: May 15, 2014 8:20 PM

Hi all,

This is actually something I've been wondering for a while... Say I have a 1-by-4 vector A and a 5-by-4 matrix B. Now I want to assign the vector A to certain rows of B, but unfortunately you can't do something like

B([1,3,5],:)=A

since the dimensions will mismatch. What I ended up doing is

B([1,3,5],:)=repmat(A,3,1)

which works but seems really ugly (and probably slow as well since I used repmat, which generates a large intermediary matrix).

So my first question: is there a better way to do this?

Another thought in a different context, though slightly related: it'll be great if the built-in element-wise multiplication (.*) and division (./ and .\) can be generalized to incorporate the function of bsxfun, so if use can do A.*B instead of repmat(A,5,1).*B or bsxfun(@times,A,B). I thought about it and it doesn't seem to introduce any ambiguity... we could also have .+ and .- instead of bsxfun(@plus) and bsxfun(@minus), and even some assignment operator like .= to solve my first problem!

My second question: is there a way to do something like this? Matlab doesn't seem to allow operator overload for built-in classes though...

Just wanted to share some of my random thoughts... But I'll appreciate it if someone bothers to answer my questions :)

Niko

Date Subject Author
5/15/14 Niko
5/15/14 Bruno Luong
5/16/14 Doug Schwarz
5/17/14 Daniel
5/17/14 Daniel
5/17/14 Bruno Luong