Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Array creation from two different sized vectors

Subject: Array creation from two different sized vectors

From: Ross

Date: 10 Jul, 2013 17:23:13

Message: 1 of 7

I am trying to get rid of the for loop in this code to make it faster, the size commands are just for your guy's reference:
size(r_loc) = length(lx) 3
size(r_prime) = 301088 3

modl_new = zeros([length(lx) , 301088 ,3]);

for i = 1:length(lx)
     modl_new(i,:,1) = r_loc(i,1) - r_prime(:,1);
     modl_new(i,:,2) = r_loc(i,2) - r_prime(:,2);
     modl_new(i,:,3) = r_loc(i,3) - r_prime(:,3);
end

Subject: Array creation from two different sized vectors

From: Jon

Date: 25 Jul, 2013 22:38:29

Message: 2 of 7


It looks like what you want to do is similar to the kron function that Matlab has for multiplication. Try using this code, which is adapted from the kron function
------------
% permute the arrays so each row becomes a page
rloc3=permute(rloc,[1,3,2]);
rpri3=permute(rpri,[3,1,2]);
% get the dimensions of the arrays
[ma,na,pa] = size(rloc3);
[mb,nb,pb] = size(rpri3);
% expand the arrays
[ia,ib] = meshgrid(1:ma,1:mb);
[ja,jb] = meshgrid(1:na,1:nb);
% sum the elements
C = rloc3(ia,ja,1:3)-rpri3(ib,jb,1:3);
-----------------
With the for loop you started with and length(lx)=500, my system took ~30 sec run. My code finished in about 11 sec.

The last line can be rather memory intensive depending on the size of rloc. During the calculation, my memory usage went from about 30% to about 80%. If you exceed your physical memory and start swapping to disk, it's much slower than the for loop. You can reduce the memory hit without much of a time penalty by doing each page separately:
---------------
C = zeros(size(rloc,1),size(rpri,1),3);
C(:,:,1) = rloc3(ia,ja,1)-rpri3(ib,jb,1);
C(:,:,2) = rloc3(ia,ja,2)-rpri3(ib,jb,2);
C(:,:,3) = rloc3(ia,ja,3)-rpri3(ib,jb,3);
---------------

My system: dual 6-core Xeon with 16GB RAM

Subject: Array creation from two different sized vectors

From: Roger Stafford

Date: 26 Jul, 2013 10:22:07

Message: 3 of 7

"Ross " <ross.dijulio@gmail.com> wrote in message <krk5a1$rtv$1@newscl01ah.mathworks.com>...
> I am trying to get rid of the for loop in this code to make it faster, the size commands are just for your guy's reference:
> size(r_loc) = length(lx) 3
> size(r_prime) = 301088 3
>
> modl_new = zeros([length(lx) , 301088 ,3]);
>
> for i = 1:length(lx)
> modl_new(i,:,1) = r_loc(i,1) - r_prime(:,1);
> modl_new(i,:,2) = r_loc(i,2) - r_prime(:,2);
> modl_new(i,:,3) = r_loc(i,3) - r_prime(:,3);
> end
- - - - - - - - - - - - -
  Use the 'bsxfun' function to expand singleton dimensions after reshaping appropriately:

modl_new = bsxfun(@minus,reshape(r_loc,[],1,3),reshape(r_prime,1,[],3));

Roger Stafford

Subject: Array creation from two different sized vectors

From: Nasser M. Abbasi

Date: 26 Jul, 2013 11:36:31

Message: 4 of 7

On 7/26/2013 5:22 AM, Roger Stafford wrote:
> "Ross " <ross.dijulio@gmail.com> wrote in message <krk5a1$rtv$1@newscl01ah.mathworks.com>...
>> I am trying to get rid of the for loop in this code to make it faster, the size
>commands are just for your guy's reference:
>> size(r_loc) = length(lx) 3
>> size(r_prime) = 301088 3
>>
>> modl_new = zeros([length(lx) , 301088 ,3]);
>>
>> for i = 1:length(lx)
>> modl_new(i,:,1) = r_loc(i,1) - r_prime(:,1);
>> modl_new(i,:,2) = r_loc(i,2) - r_prime(:,2);
>> modl_new(i,:,3) = r_loc(i,3) - r_prime(:,3);
>> end
> - - - - - - - - - - - - -
> Use the 'bsxfun' function to expand singleton dimensions after reshaping appropriately:
>
> modl_new = bsxfun(@minus,reshape(r_loc,[],1,3),reshape(r_prime,1,[],3));
>
> Roger Stafford
>

hi Roger,

Which do you think is faster? The loop, or your method? Was just
wondering on the cost of reshape all the matrices? But on
the other hand, loops are 'supposed' to be slower, but
as shown, there is no reshaping involved in them, and I assume
reshaping large matrices will be expensive?

I was just wondering which method will be faster on large data.

--Nasser

Subject: Array creation from two different sized vectors

From: dpb

Date: 26 Jul, 2013 13:33:42

Message: 5 of 7

On 7/26/2013 6:36 AM, Nasser M. Abbasi wrote:
...

> Which do you think is faster? The loop, or your method? Was just
> wondering on the cost of reshape all the matrices? But on
> the other hand, loops are 'supposed' to be slower, but
> as shown, there is no reshaping involved in them, and I assume
> reshaping large matrices will be expensive?
>
> I was just wondering which method will be faster on large data.

So make some dummy data and time it...don't expect somebody else to do
routine work for you.

NB that to have any meaningful results will have to do it from m-file as
JIT will get scrogged from command window.

--

Subject: Array creation from two different sized vectors

From: Steven_Lord

Date: 26 Jul, 2013 14:23:45

Message: 6 of 7



"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:kstmvu$td0$1@speranza.aioe.org...
> On 7/26/2013 5:22 AM, Roger Stafford wrote:
>> "Ross " <ross.dijulio@gmail.com> wrote in message
>> <krk5a1$rtv$1@newscl01ah.mathworks.com>...
>>> I am trying to get rid of the for loop in this code to make it faster,
>>> the size
>>commands are just for your guy's reference:
>>> size(r_loc) = length(lx) 3
>>> size(r_prime) = 301088 3
>>>
>>> modl_new = zeros([length(lx) , 301088 ,3]);
>>>
>>> for i = 1:length(lx)
>>> modl_new(i,:,1) = r_loc(i,1) - r_prime(:,1);
>>> modl_new(i,:,2) = r_loc(i,2) - r_prime(:,2);
>>> modl_new(i,:,3) = r_loc(i,3) - r_prime(:,3);
>>> end
>> - - - - - - - - - - - - -
>> Use the 'bsxfun' function to expand singleton dimensions after
>> reshaping appropriately:
>>
>> modl_new = bsxfun(@minus,reshape(r_loc,[],1,3),reshape(r_prime,1,[],3));
>>
>> Roger Stafford
>>
>
> hi Roger,
>
> Which do you think is faster?

There's an easy way to check ;)

> The loop, or your method? Was just
> wondering on the cost of reshape all the matrices? But on
> the other hand, loops are 'supposed' to be slower,

*sigh*

The message was at one time that simple. It is no longer that simple with
improvements we've made to MATLAB in the past *checks when release R13 came
out* eleven plus years.

> but
> as shown, there is no reshaping involved in them, and I assume
> reshaping large matrices will be expensive?

You can test that assumption.

>> A = reshape(1:prod(5:13), 5:13);
>> tic; B = reshape(A, 13:-1:5); toc
Elapsed time is 0.113501 seconds.

I wouldn't call a tenth of a second to reshape a matrix with about 260
million elements "expensive." And that was from the command line; inside a
function and/or if you move the creation of 13:-1:5 taking outside the
TIC/TOC, it will even faster. RESHAPE doesn't change the data or the
organization of the data like other functions do (TRANSPOSE and PERMUTE for
example.)

> I was just wondering which method will be faster on large data.

Try it! I'm guessing that neither method will be faster across the whole
spectrum of data sizes; there will likely be a cutover point where the two
performance curves cross.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Array creation from two different sized vectors

From: Jon

Date: 26 Jul, 2013 20:35:11

Message: 7 of 7

Well, I learned a new trick today. My method works on arrays without singleton dimensions and can be extended to 3-D or more, but bsxfun is faster for this case and is applicable in several scripts I'm working with.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us