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:
Indexing a range of array values using a matrix?

Subject: Indexing a range of array values using a matrix?

From: Evan Ruzanski

Date: 10 Mar, 2012 19:48:30

Message: 1 of 6

Hello,

I'm looking to create the following output without using loops. Let's say I have some array of data:

>> arr = [1 7 3 2 1 5 6 7 8 4]'

and a matrix that defines a range of indices related to the array:

>> arrLocs = [2 4;6 9]

whose first column represents the start values and second column represents the end values of the range to extract from the data array. So using this example I would like to see the following output:

>> out = [0 0;1 0;1 0;1 0;0 0;0 1;0 1;0 1;0 1;0 0]

Additionally, I would like to return an array of maximum values within the ranges:

>> outMax = [7 8]

Again, I can do this with loops no problem but I'm trying to avoid loops. Also, the matrix will scale in rows, so the solution should be able to handle as well something like:

>> arrLocs2 = [2 4;6 8;10 10]

>> out2 = [0 0 0;1 0 0;1 0 0;1 0 0;0 0 0;0 1 0;0 1 0;0 1 0;0 0 0;0 0 1]

Many sincere thanks and have a wonderful day...

Subject: Indexing a range of array values using a matrix?

From: Matt J

Date: 10 Mar, 2012 22:29:11

Message: 2 of 6

"Evan Ruzanski" wrote in message <jjgb6e$qfh$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm looking to create the following output without using loops. Let's say I have some array of data:
>
> >> arr = [1 7 3 2 1 5 6 7 8 4]'
>
> and a matrix that defines a range of indices related to the array:
>
> >> arrLocs = [2 4;6 9]
>
> whose first column represents the start values and second column represents the end values of the range to extract from the data array. So using this example I would like to see the following output:
>
> >> out = [0 0;1 0;1 0;1 0;0 0;0 1;0 1;0 1;0 1;0 0]
================

I could be missing something obvious, but this makes little sense to me. Your out variable contains lots of zeros even though your input data arr contain none.


 

> Again, I can do this with loops no problem but I'm trying to avoid loops.
===============

Elaborate on why you're trying to avoid loops. It seems like the logical thing to use.

Subject: Indexing a range of array values using a matrix?

From: Roger Stafford

Date: 11 Mar, 2012 01:08:13

Message: 3 of 6

"Evan Ruzanski" wrote in message <jjgb6e$qfh$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm looking to create the following output without using loops. Let's say I have some array of data:
>
> >> arr = [1 7 3 2 1 5 6 7 8 4]'
>
> and a matrix that defines a range of indices related to the array:
>
> >> arrLocs = [2 4;6 9]
>
> whose first column represents the start values and second column represents the end values of the range to extract from the data array. So using this example I would like to see the following output:
>
> >> out = [0 0;1 0;1 0;1 0;0 0;0 1;0 1;0 1;0 1;0 0]
>
> Additionally, I would like to return an array of maximum values within the ranges:
>
> >> outMax = [7 8]
>
> Again, I can do this with loops no problem but I'm trying to avoid loops. Also, the matrix will scale in rows, so the solution should be able to handle as well something like:
>
> >> arrLocs2 = [2 4;6 8;10 10]
>
> >> out2 = [0 0 0;1 0 0;1 0 0;1 0 0;0 0 0;0 1 0;0 1 0;0 1 0;0 0 0;0 0 1]
>
> Many sincere thanks and have a wonderful day...
- - - - - - - - -
 out = (bsxfun(@le.arrLocs(:,1),1:length(arr)) & ...
        bsxfun(@ge,arrLocs(:,2),1:length(arr))).';

Roger Stafford

Subject: Indexing a range of array values using a matrix?

From: Evan Ruzanski

Date: 11 Mar, 2012 02:44:26

Message: 4 of 6

"Matt J" wrote in message <jjgkjn$ngt$1@newscl01ah.mathworks.com>...
> "Evan Ruzanski" wrote in message <jjgb6e$qfh$1@newscl01ah.mathworks.com>...
> > Hello,
> >
> > I'm looking to create the following output without using loops. Let's say I have some array of data:
> >
> > >> arr = [1 7 3 2 1 5 6 7 8 4]'
> >
> > and a matrix that defines a range of indices related to the array:
> >
> > >> arrLocs = [2 4;6 9]
> >
> > whose first column represents the start values and second column represents the end values of the range to extract from the data array. So using this example I would like to see the following output:
> >
> > >> out = [0 0;1 0;1 0;1 0;0 0;0 1;0 1;0 1;0 1;0 0]
> ================
>
> I could be missing something obvious, but this makes little sense to me. Your out variable contains lots of zeros even though your input data arr contain none.
>
>
>
>
> > Again, I can do this with loops no problem but I'm trying to avoid loops.
> ===============
>
> Elaborate on why you're trying to avoid loops. It seems like the logical thing to use.

Hi Matt J,

Thank you for the response. I found the binary output to be necessary to create a single output matrix (to account for ranges of different lengths); the ones tell me where the values are located and the zeros essentially act as placeholders to be able to build the output matrix. I don't know how to build a matrix in Matlab with different column lengths.

I found the solution with loops, but I wanted to see how to do it without and learn a new way to solve the problem.

Subject: Indexing a range of array values using a matrix?

From: Evan Ruzanski

Date: 11 Mar, 2012 03:01:41

Message: 5 of 6

"Roger Stafford" wrote in message <jjgttt$k3h$1@newscl01ah.mathworks.com>...
> - - - - - - - - -
> out = (bsxfun(@le.arrLocs(:,1),1:length(arr)) & ...
> bsxfun(@ge,arrLocs(:,2),1:length(arr))).';
>
> Roger Stafford

Thanks Roger, this is exactly what I was looking for. I just recently found the 'bsxfun' function with '@le' and '@ge' but wasn't using it right...

Subject: Indexing a range of array values using a matrix?

From: Steven_Lord

Date: 12 Mar, 2012 14:14:44

Message: 6 of 6



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:jjgkjn$ngt$1@newscl01ah.mathworks.com...
> "Evan Ruzanski" wrote in message
> <jjgb6e$qfh$1@newscl01ah.mathworks.com>...
>> Hello,
>>
>> I'm looking to create the following output without using loops. Let's say
>> I have some array of data:
>>
>> >> arr = [1 7 3 2 1 5 6 7 8 4]'
>>
>> and a matrix that defines a range of indices related to the array:
>>
>> >> arrLocs = [2 4;6 9]
>>
>> whose first column represents the start values and second column
>> represents the end values of the range to extract from the data array. So
>> using this example I would like to see the following output:
>>
>> >> out = [0 0;1 0;1 0;1 0;0 0;0 1;0 1;0 1;0 1;0 0]
> ================
>
> I could be missing something obvious, but this makes little sense to me.
> Your out variable contains lots of zeros even though your input data arr
> contain none.

The first row of out represents the logical indices that would extract the
elements between element arrLocs(1, 1) and arrLocs(1, 2) and similarly for
the second row. The OP's looking to go from indices to a mask. They haven't
used it to try to index into arr yet.

>> Again, I can do this with loops no problem but I'm trying to avoid loops.
> ===============
>
> Elaborate on why you're trying to avoid loops. It seems like the logical
> thing to use.

Particularly if that FOR loop will be from 1 to 2, as it will in this case
since size(arrLocs, 1) is 2.


A slightly stronger way to say what you did, Matt: FOR is not a four-letter
(key)word in MATLAB anymore.

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

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