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 for simple algorithm

Subject: help for simple algorithm

From: kk aKI

Date: 8 Feb, 2010 20:10:06

Message: 1 of 8

Hi!
I am very new to matlab! or i should say programming. I have a quick question
suppose I have Vector of 1000 elements say 1 by 1000. Now i want to replace every 2nd and 3rd element by zero! How can i do it. Again same algorithm i can apply for making every 5 elements such that 2 to 6 will be zero then 7th will be some number same goes on and on till 1000

Thanks

Aki

Subject: help for simple algorithm

From: ade77

Date: 8 Feb, 2010 21:14:04

Message: 2 of 8

"kk aKI" <akikumar1983@gmail.com> wrote in message <hkpr2u$nq9$1@fred.mathworks.com>...
> Hi!
> I am very new to matlab! or i should say programming. I have a quick question
> suppose I have Vector of 1000 elements say 1 by 1000. Now i want to replace every 2nd and 3rd element by zero! How can i do it. Again same algorithm i can apply for making every 5 elements such that 2 to 6 will be zero then 7th will be some number same goes on and on till 1000
>
> Thanks
>
> Aki

since you are new, it is imperative for you to understand some useful functions.
type doc reshape on the command prompt.Also check the documentation on how to manipulate arrays
Your question is not very clear.
Let us start with a simple example of vector = [1 2 3 4 5 6 7 8 9 10];
you want to make 2nd, 3rd, 5th,6th,8th,9th to be zero.
There are many ways, you can use a loop.

Another way is to append your vector with zero first to be a multiple of 3,then later remove the appended zeros.
vector = 1:10;
vector = [vector zeros(1,2)]; %this will ensure a multiple of 3, you will later remove this appended zeros.

vector1 = reshape(vector,3,[]); % 3 rows, MATLAB figures out how many columns
vector1(2:3,:) = 0; % make the 2nd,3rd, 5th, 6th row etc zeros
vector1 = vector1(:); % back to your vector
vector1(end-1:end) = []; %remove the appended zeros above


I am not sure if this is the solution you are looking for , because I am not very clear on the question

Subject: help for simple algorithm

From: ImageAnalyst

Date: 8 Feb, 2010 21:21:59

Message: 3 of 8

How about this:

a = round(100 * rand(1,50)) % Generate sample data

a(1:2:end) = 0 % Set every second one = 0
a(1:3:end) = 0 % Set every third one = 0

Subject: help for simple algorithm

From: TideMan

Date: 8 Feb, 2010 21:38:46

Message: 4 of 8

On Feb 9, 10:21 am, ImageAnalyst <imageanal...@mailinator.com> wrote:
> How about this:
>
> a = round(100 * rand(1,50))  % Generate sample data
>
> a(1:2:end) = 0  % Set every second one = 0
> a(1:3:end) = 0  % Set every third one = 0

Alternatively:
n=50; % No of data
inc=6; % Extract every 6th data point
a=round(100*rand(n,1)); % Generate sample data
b=zeros(n,1); % Create a vector of zeros
b(1:inc:end)=a(1:inc:end); % Populate every inc-th address

Subject: help for simple algorithm

From: kk aKI

Date: 9 Feb, 2010 00:01:22

Message: 5 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e00aa366-e6b0-4247-bb7b-d50c576b7e6a@f12g2000yqn.googlegroups.com>...
> How about this:
>
> a = round(100 * rand(1,50)) % Generate sample data
>
> a(1:2:end) = 0 % Set every second one = 0
> a(1:3:end) = 0 % Set every third one = 0

Columns 1 through 16

     0 45 0 0 0 68 0 73 0 0 0 45 0 89 0 0

  Columns 17 through 32

     0 23 0 91 0 0 0 8 0 19 0 0 0 99 0 34

  Columns 33 through 48

     0 0 0 59 0 4 0 0 0 26 0 87 0 0 0 66

  Columns 49 through 50

     0 1
So 1st is zero and 2nd , 3rd, 4th is zero......what i was looking for every two element to become zero after 1..like 1 has some value then 2 and 3 is zero again 4 has some value

Subject: help for simple algorithm

From: kk aKI

Date: 9 Feb, 2010 00:01:22

Message: 6 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e00aa366-e6b0-4247-bb7b-d50c576b7e6a@f12g2000yqn.googlegroups.com>...
> How about this:
>
> a = round(100 * rand(1,50)) % Generate sample data
>
> a(1:2:end) = 0 % Set every second one = 0
> a(1:3:end) = 0 % Set every third one = 0

Columns 1 through 16

     0 45 0 0 0 68 0 73 0 0 0 45 0 89 0 0

  Columns 17 through 32

     0 23 0 91 0 0 0 8 0 19 0 0 0 99 0 34

  Columns 33 through 48

     0 0 0 59 0 4 0 0 0 26 0 87 0 0 0 66

  Columns 49 through 50

     0 1
So 1st is zero and 2nd , 3rd, 4th is zero......what i was looking for every two element to become zero after 1..like 1 has some value then 2 and 3 is zero again 4 has some value

Subject: help for simple algorithm

From: kk aKI

Date: 9 Feb, 2010 00:06:03

Message: 7 of 8

Thanks it works for me :)

TideMan <mulgor@gmail.com> wrote in message <8670609c-497a-4ba6-a2f1-b8024ec104b5@g28g2000prb.googlegroups.com>...
> On Feb 9, 10:21 am, ImageAnalyst <imageanal...@mailinator.com> wrote:
> > How about this:
> >
> > a = round(100 * rand(1,50))  % Generate sample data
> >
> > a(1:2:end) = 0  % Set every second one = 0
> > a(1:3:end) = 0  % Set every third one = 0
>
> Alternatively:
> n=50; % No of data
> inc=6; % Extract every 6th data point
> a=round(100*rand(n,1)); % Generate sample data
> b=zeros(n,1); % Create a vector of zeros
> b(1:inc:end)=a(1:inc:end); % Populate every inc-th address

Subject: help for simple algorithm

From: ImageAnalyst

Date: 9 Feb, 2010 03:46:05

Message: 8 of 8

On Feb 8, 7:01 pm, "kk aKI" <akikumar1...@gmail.com> wrote:
[snip]
>      0     1
> So 1st is zero and 2nd , 3rd, 4th is zero......what i was looking for every two element to become zero after 1..like 1 has some value then 2 and 3 is zero again 4 has some value
--------------------------------------------------------
You can start at any value. If you don't want to start with 1, then
start with 2 or 3 or whatever you want, like this:

a(2:2:end) = 0;
a(3:2:end) = 0; or
a(42:3:end) = 0; or whatever you want.
basically it goes like this:
a(startIndex:increment:end) = 0;

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