MATLAB help

  • MATLAB
  • Thread starter Meurig
  • Start date
  • #1
6
0

Main Question or Discussion Point

Hi everyone,

I'm trying to make my MATLAB code run as efficiently as possible.

II have a 2xN vector designated like so:
acc1(1,:) = A*rc(1,:);
acc1(2,:) = B*rc(2,:);

where A and B are scalars,.

Is it possible to combine these two into a single line of code?

Cheers
 

Answers and Replies

  • #2
131
0
If acc1 and rc have only 2 rows, then you can use: acc1=bsxfun(@times, rc, [A; B]);

If acc1 and/or rc have more than 2 rows, then: acc1(1:2, :)=bsxfun(@times, rc(1:2, :), [A; B]);

Documentation for http://www.mathworks.com/help/techdoc/ref/bsxfun.html"

Another way to do it (if your version of Matlab doesn't have bsxfun): acc1=rc.*repmat([A; B], 1, size(rc, 2));
 
Last edited by a moderator:
  • #3
6
0
Thanks very much!
 

Related Threads on MATLAB help

  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
6
Views
7K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
3
Views
5K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
4K
Top