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:
Cropping a range of data from the matrix

Subject: Cropping a range of data from the matrix

From: Recep Ozdemir

Date: 7 Jul, 2011 20:18:56

Message: 1 of 3

Hi everybody,

I am a beginner matlab user and I need your immediate help for the
following case;

lets say I have an X matrix

X =

     1 2
     2 4
     3 6
     4 8
     5 10
     6 12
     7 14
     8 16
     9 18
    10 20

I need to truncate this matrix and only need to obtain the values in
column 1 when the values in column 2 are between "6" and "14"

So, in this case, my final matrix should be ;

X =

     3 6
     4 8
     5 10
     6 12
     7 14

Do you know which matlab commands should I have to use for this task

Thanks very much

Recep

Subject: Cropping a range of data from the matrix

From: Jeff

Date: 7 Jul, 2011 21:47:09

Message: 2 of 3

Recep Ozdemir <recep.ozdemir64@gmail.com> wrote in message <471ec6cc-245d-4857-bbe1-a4d6cd9f1084@v7g2000vbk.googlegroups.com>...
> Hi everybody,
>
> I am a beginner matlab user and I need your immediate help for the
> following case;
>
> lets say I have an X matrix
>
> X =
>
> 1 2
> 2 4
> 3 6
> 4 8
> 5 10
> 6 12
> 7 14
> 8 16
> 9 18
> 10 20
>
> I need to truncate this matrix and only need to obtain the values in
> column 1 when the values in column 2 are between "6" and "14"
>
> So, in this case, my final matrix should be ;
>
> X =
>
> 3 6
> 4 8
> 5 10
> 6 12
> 7 14
>
> Do you know which matlab commands should I have to use for this task
>
> Thanks very much
>
> Recep

If the values in column 1 are just an indexing of the rows, then you can just use:
idx = find(x(:,2) >= 6 & x(:,2) <=14);

If the values in column 1 aren't an index, use:
idx = (x(:,2) >= 6 & x(:,2) <=14);
out = idx .* x(:,1) ;
out2 = out(out > 0);

Subject: Cropping a range of data from the matrix

From: Roger Stafford

Date: 7 Jul, 2011 22:39:10

Message: 3 of 3

Recep Ozdemir <recep.ozdemir64@gmail.com> wrote in message <471ec6cc-245d-4857-bbe1-a4d6cd9f1084@v7g2000vbk.googlegroups.com>...
> Hi everybody,
>
> I am a beginner matlab user and I need your immediate help for the
> following case;
>
> lets say I have an X matrix
>
> X =
>
> 1 2
> 2 4
> 3 6
> 4 8
> 5 10
> 6 12
> 7 14
> 8 16
> 9 18
> 10 20
>
> I need to truncate this matrix and only need to obtain the values in
> column 1 when the values in column 2 are between "6" and "14"
>
> So, in this case, my final matrix should be ;
>
> X =
>
> 3 6
> 4 8
> 5 10
> 6 12
> 7 14
>
> Do you know which matlab commands should I have to use for this task
>
> Thanks very much
>
> Recep
- - - - - - - -
  It can be done with logical indexing in just one line.

 X = X(6<=X(:,2)&X(:,2)<=14,:);

Roger Stafford

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