Got Questions? Get Answers.
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:
vectors

Subject: vectors

From: Konst

Date: 14 Feb, 2011 19:38:03

Message: 1 of 10

I have a random vector A=[ 2 3 1 4 3 1...]. I need to create a new vector like this:
(i=0:N eg N=100)
For the 2i+1 cells put in the new vector B as many zeros as the value in A says.
For the 2i+2 cells pun in the new vector B as many ones as the corresponding value in A)
That is for the above A, I need to make this B:
B=[0 0 1 1 1 0 1 1 1 1 0 0 0 1...] etc..
I don't know how else to describe it but this will give you the idea. Can anyone help?Please...

Subject: vectors

From: Sean de

Date: 14 Feb, 2011 19:53:03

Message: 2 of 10

"Konst " <konstance1@hotmail.com> wrote in message <ijc0ar$i33$1@fred.mathworks.com>...
> I have a random vector A=[ 2 3 1 4 3 1...]. I need to create a new vector like this:
> (i=0:N eg N=100)
> For the 2i+1 cells put in the new vector B as many zeros as the value in A says.
> For the 2i+2 cells pun in the new vector B as many ones as the corresponding value in A)
> That is for the above A, I need to make this B:
> B=[0 0 1 1 1 0 1 1 1 1 0 0 0 1...] etc..
> I don't know how else to describe it but this will give you the idea. Can anyone help?Please...

One, probably not optimal, way:

B = cell2mat(cellfun(@(x)[zeros(1,x(1)), ones(1,x(2))],mat2cell(A,1,2*ones(1,length(A)/2)),'uni',false));


But this will only work for A of even length. I'll leave it to you to figure out how to incorporate odd length A's.

Subject: vectors

From: Konst

Date: 14 Feb, 2011 20:11:03

Message: 3 of 10

It returns errors about missing operators and when i Try to correct them it return an error that function wasn't found(????).Thanks though. I'll try it again.

Subject: vectors

From: Bruno Luong

Date: 14 Feb, 2011 20:17:04

Message: 4 of 10

"Konst " <konstance1@hotmail.com> wrote in message <ijc0ar$i33$1@fred.mathworks.com>...
> I have a random vector A=[ 2 3 1 4 3 1...]. I need to create a new vector like this:
> (i=0:N eg N=100)
> For the 2i+1 cells put in the new vector B as many zeros as the value in A says.
> For the 2i+2 cells pun in the new vector B as many ones as the corresponding value in A)
> That is for the above A, I need to make this B:
> B=[0 0 1 1 1 0 1 1 1 1 0 0 0 1...] etc..

s = cumsum([0 A])+1;
% clear b
b(s(1:2:end)) = -1
b(s(2:2:end)) = +1
b = cumsum(b(1:end-1))+1

% Bruno

Subject: vectors

From: Matt Fig

Date: 14 Feb, 2011 20:41:04

Message: 5 of 10

An old-fashioned approach:

y = A==0; % Keep it general
A(y) = [];
L = length(A);
x(1:2:L) = 0;
x(2:2:L) = 1;
y = cumsum(A);
idx = zeros(1,y(L));
idx([1 y(1:L-1)+1]) = 1;
idx = cumsum(idx);
y = x(idx);

Subject: vectors

From: Matt Fig

Date: 14 Feb, 2011 20:47:03

Message: 6 of 10

I had my order of ops wrong to handle the general case to include zeros in A.

L = length(A);
x(1:2:L) = 0;
x(2:2:L) = 1;
y = A==0; % Keep it general
A(y) = [];
x(y) = [];
L = length(A);
y = cumsum(A);
idx = zeros(1,y(L));
idx([1 y(1:L-1)+1]) = 1;
idx = cumsum(idx);
y = x(idx);

Subject: vectors

From: Konst

Date: 14 Feb, 2011 21:26:02

Message: 7 of 10

"Matt Fig" wrote in message <ijc4c7$d1i$1@fred.mathworks.com>...
> I had my order of ops wrong to handle the general case to include zeros in A.
>
> L = length(A);
> x(1:2:L) = 0;
> x(2:2:L) = 1;
> y = A==0; % Keep it general
> A(y) = [];
> x(y) = [];
> L = length(A);
> y = cumsum(A);
> idx = zeros(1,y(L));
> idx([1 y(1:L-1)+1]) = 1;
> idx = cumsum(idx);
> y = x(idx);

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> C:\MATLAB6p1\work\pdf1.m
On line 23 ==> idx([1 y(1:L-1)+1]) = 1;

The same error returns for Bruno's way:
On line 16 ==> s = cumsum([0 A])+1;

Any ideas why?
PS: Thank you both!

Subject: vectors

From: Bruno Luong

Date: 14 Feb, 2011 21:27:04

Message: 8 of 10

% A slightly different method

s = cumsum(A);
% b = zeros(1,s(end));
b(s+1) = (-1).^(0:length(s)-1)
b(end) = [];
b = cumsum(b)

% Bruno

Subject: vectors

From: Steven_Lord

Date: 14 Feb, 2011 21:36:03

Message: 9 of 10



"Konst " <konstance1@hotmail.com> wrote in message
news:ijc28n$p3e$1@fred.mathworks.com...
> It returns errors about missing operators and when i Try to correct them
> it return an error that function wasn't found(????).Thanks though. I'll
> try it again.

Yes; Sean used functionality (anonymous functions, the ability of CELLFUN to
accept arbitrary function handles as input) that was introduced after MATLAB
6.1. The error message you posted later in this thread indicates that's the
version you're using.

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

Subject: vectors

From: Konst

Date: 14 Feb, 2011 21:37:05

Message: 10 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ijc6n8$h5d$1@fred.mathworks.com>...
> % A slightly different method
>
> s = cumsum(A);
> % b = zeros(1,s(end));
> b(s+1) = (-1).^(0:length(s)-1)
> b(end) = [];
> b = cumsum(b)
>
> % Bruno

Thanks It works perfectly!..

Tags for this Thread

No tags are associated with 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