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:
help with a vector problem

Subject: help with a vector problem

From: ade77

Date: 21 Jan, 2010 19:19:04

Message: 1 of 5

I have some column vectors:
N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11.
these vectors are of different length.

I want to add the elements of these vectors, in multiples of 3, when the remaining is not 3, then I just add it like that.

From the example above,
I will have :
sum(N1) + sum(N2) + sum(N3)
sum(N4) + sum(N5) + sum(N6)
sum(N7) + sum(N8) + sum(N9)
sum(N10) + sum(N11) % since two vectors are left, I will sum the two like that

So I will end up with a vector of 4 by 1.
I am trying to do this without a loop.

Thanks for any help

Subject: help with a vector problem

From: Jan Simon

Date: 22 Jan, 2010 10:47:03

Message: 2 of 5

Dear ade77!

> I have some column vectors:
> N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11.
> these vectors are of different length.
>
> I want to add the elements of these vectors, in multiples of 3, when the remaining is not 3, then I just add it like that.
>
> From the example above,
> I will have :
> sum(N1) + sum(N2) + sum(N3)
> sum(N4) + sum(N5) + sum(N6)
> sum(N7) + sum(N8) + sum(N9)
> sum(N10) + sum(N11) % since two vectors are left, I will sum the two like that

This depends on what exactly this means "I have some column vectors".
If you have them as different variables, it is a mess, because the algorithm cannot know the names of the variables without nasty calls to WHOS etc.

If you have the vectors as elements of a cell, it is much easier. But I would'nt try it without loops. The single sums can be built like:
   C = sum(cellfun(@sum, N(1:3)))

There will be more vectorized methods, but the programming and debug time will exceed the processing time by far - I assume. It is better to program a loop and start the program before the lunch than to start a smart supervectorized method tomorrow and struggle with bugs for hours if tiny generalizations are needed.

Or perhaps let Bruno struggle, if he likes to.

Kind regards, Jan

Kind regards, Jan

Subject: help with a vector problem

From: Jos (10584)

Date: 22 Jan, 2010 12:07:02

Message: 3 of 5

"ade77 " <ade100a@gmail.com> wrote in message <hja9b8$juv$1@fred.mathworks.com>...
> I have some column vectors:
> N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11.
> these vectors are of different length.
>
> I want to add the elements of these vectors, in multiples of 3, when the remaining is not 3, then I just add it like that.
>
> From the example above,
> I will have :
> sum(N1) + sum(N2) + sum(N3)
> sum(N4) + sum(N5) + sum(N6)
> sum(N7) + sum(N8) + sum(N9)
> sum(N10) + sum(N11) % since two vectors are left, I will sum the two like that
>
> So I will end up with a vector of 4 by 1.
> I am trying to do this without a loop.
>
> Thanks for any help

I hope you have stored each column vector as a separate cell in a single cell array (option 1), or are the really independent variables (option 2)?

Option 1) a single cell array
% some test data
  for ii=1:11,C{ii} = (1:ceil(5*rand)).' ; end
  NSTEPS = 3 ;

% engine
  L = cellfun('length',C) ;
  idx = zeros(sum(L),1) ;
  idx(cumsum([1 L(1:end-1)])) = 1 ;
  idx = cumsum(idx) ;
  idx = ceil(idx ./ NSTEPS) ;
  C2 = cat(1,C{:}) ;
  disp([idx C2])
  result = accumarray(idx,C2)

Option 2) - 11 variables
Transform your data into option 1 !!

And here is a big tip
sum(n1) + sum(n2) + sum (n3) = sum([n1 ; n2 ;n3]) % for column vectors

hth
Jos

Subject: help with a vector problem

From: ade77

Date: 22 Jan, 2010 22:17:02

Message: 4 of 5

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hjc4d6$96v$1@fred.mathworks.com>...
> "ade77 " <ade100a@gmail.com> wrote in message <hja9b8$juv$1@fred.mathworks.com>...
> > I have some column vectors:
> > N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N11.
> > these vectors are of different length.
> >
> > I want to add the elements of these vectors, in multiples of 3, when the remaining is not 3, then I just add it like that.
> >
> > From the example above,
> > I will have :
> > sum(N1) + sum(N2) + sum(N3)
> > sum(N4) + sum(N5) + sum(N6)
> > sum(N7) + sum(N8) + sum(N9)
> > sum(N10) + sum(N11) % since two vectors are left, I will sum the two like that
> >
> > So I will end up with a vector of 4 by 1.
> > I am trying to do this without a loop.
> >
> > Thanks for any help
>
> I hope you have stored each column vector as a separate cell in a single cell array (option 1), or are the really independent variables (option 2)?
>
> Option 1) a single cell array
> % some test data
> for ii=1:11,C{ii} = (1:ceil(5*rand)).' ; end
> NSTEPS = 3 ;
>
> % engine
> L = cellfun('length',C) ;
> idx = zeros(sum(L),1) ;
> idx(cumsum([1 L(1:end-1)])) = 1 ;
> idx = cumsum(idx) ;
> idx = ceil(idx ./ NSTEPS) ;
> C2 = cat(1,C{:}) ;
> disp([idx C2])
> result = accumarray(idx,C2)
>
> Option 2) - 11 variables
> Transform your data into option 1 !!
>
> And here is a big tip
> sum(n1) + sum(n2) + sum (n3) = sum([n1 ; n2 ;n3]) % for column vectors
>
> hth
> Jos

Sorry for the late reply.
Yes it is stored as a single cell array, each in seperate cell.
Jan and Jos, Thank you for the reply and the solution.

Subject: help with a vector problem

From: ImageAnalyst

Date: 23 Jan, 2010 02:11:04

Message: 5 of 5

ade77:
Assuming your N's are in an array, just use the conv() (convolution)
function and then subsample to get every third element. Conv() has a
loop inside but it doesn't sum the whole thing each time - it's smart
about how to do it. Just two lines, something like this

N = rand(1,11) % Create some sample data
windowSize = 3;

% Then process it according to ade77's rules.
N_Blurred = conv(N, ones(1, windowSize )/windowSize )
ade77Answer = N_Blurred(windowSize :windowSize :end)

This is probably your most efficient way of doing it especially for
large averaging windows and large vector lengths.
Good luck,
ImageAnalyst

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