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:
Selecting rows of an array with like terms

Subject: Selecting rows of an array with like terms

From: Al

Date: 24 May, 2011 16:55:20

Message: 1 of 4

Hello,
I am looking for a simple method to select the rows of an array in which all of the numerical values in a particular column match. I tried writing a logical loop to separate and build new sub-arrays but it seems too complex for such a simple operation that there must be a built-in command to execute this task.

For example: If I have an array in which I need to average the rows that share the same value of column 1. (-0.0590 for rows 1 & 2 and -0.1180 for rows 3-6).

  -0.0590 14.8655 5.0733
  -0.0590 21.3307 -1.2717
  -0.1180 32.2026 -1.3226
  -0.1180 12.2975 -5.9853
  -0.1180 34.9242 1.4463
  -0.1180 35.4696 1.2799

Once I can get those rows isolated I can just use the "mean" command to give me the arithmetic average. Then I can concatenate them into a final array.

I cant handpick the rows or use the "sort" command since the values of column 1 are numerous and not predictable.

Thanks!
Al

Subject: Selecting rows of an array with like terms

From: dpb

Date: 24 May, 2011 17:33:21

Message: 2 of 4

On 5/24/2011 11:55 AM, Al wrote:
> Hello,
> I am looking for a simple method to select the rows of an array in which
> all of the numerical values in a particular column match. I tried
> writing a logical loop to separate and build new sub-arrays but it seems
> too complex for such a simple operation that there must be a built-in
> command to execute this task.

look at

doc ismember

or just use logical addressing...you may need to "fuzzify" the
comparison on floating point values to find "close enough" matches
instead of exact bit-matching.

>
> For example: If I have an array in which I need to average the rows that
> share the same value of column 1. (-0.0590 for rows 1 & 2 and -0.1180
> for rows 3-6).
...

m = mean(x(x(:,1)==0.059)); % salt to suit, of course...

--

Subject: Selecting rows of an array with like terms

From: dpb

Date: 24 May, 2011 17:55:56

Message: 3 of 4

On 5/24/2011 12:33 PM, dpb wrote:
> On 5/24/2011 11:55 AM, Al wrote:
>> Hello,
>> I am looking for a simple method to select the rows of an array in which
>> all of the numerical values in a particular column match. I tried
>> writing a logical loop to separate and build new sub-arrays but it seems
>> too complex for such a simple operation that there must be a built-in
>> command to execute this task.
>
> look at
>
> doc ismember
>
> or just use logical addressing...you may need to "fuzzify" the
> comparison on floating point values to find "close enough" matches
> instead of exact bit-matching.
...

Oh, meant to add--

You may find

doc unique

of interest in determining the actual values in any given column that
can then loop over to generalize the selection process...

--

Subject: Selecting rows of an array with like terms

From: Donald

Date: 27 May, 2011 05:22:04

Message: 4 of 4

"Al" wrote in message <irgnto$310$1@newscl01ah.mathworks.com>...
> Hello,
> I am looking for a simple method to select the rows of an array in which all of the numerical values in a particular column match. I tried writing a logical loop to separate and build new sub-arrays but it seems too complex for such a simple operation that there must be a built-in command to execute this task.
>
> For example: If I have an array in which I need to average the rows that share the same value of column 1. (-0.0590 for rows 1 & 2 and -0.1180 for rows 3-6).
>
> -0.0590 14.8655 5.0733
> -0.0590 21.3307 -1.2717
> -0.1180 32.2026 -1.3226
> -0.1180 12.2975 -5.9853
> -0.1180 34.9242 1.4463
> -0.1180 35.4696 1.2799
>
> Once I can get those rows isolated I can just use the "mean" command to give me the arithmetic average. Then I can concatenate them into a final array.
>
> I cant handpick the rows or use the "sort" command since the values of column 1 are numerous and not predictable.
>
> Thanks!
> Al


A simple way would be to use accumarray. Since your first column contains floating point numbers you first have to find the unique values and the corresponding indices. Then you apply the indices as an input into accumarray.


  data = [-0.0590 14.8655 5.0733
  -0.0590 21.3307 -1.2717
  -0.1180 32.2026 -1.3226
  -0.1180 12.2975 -5.9853
  -0.1180 34.9242 1.4463
  -0.1180 35.4696 1.2799 ];

%Find unique elements in first column of data set. b1 contains the unique
%values, n1 contains the indices into b1 for the value in the column.
[b1, m1, n1] = unique(data(:,1));

%Compute the mean for each group of unique elements that appear in the
%first column. meanVec(i) is the mean of all elements in the second column
%where the first column equals b1(i)
meanVec = accumarray( n1, data(:,2),[],@mean);

meanVec = 18.0981 28.723475

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