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:
Conditional Maximum

Subject: Conditional Maximum

From: Rahat

Date: 24 Nov, 2012 18:07:07

Message: 1 of 3

I have 3 columns in my database:

ID Year Tenure
12 1992 1
12 1993 2
12 1994 3
12 1995 4
225 2001 1
225 2002 2
225 2003 3
225 2004 4
225 2005 5
225 2006 6
3045 2009 1
3045 2010 2
3045 2011 3


I want to keep IDs with a tenure >=5 years. For that matter I want to create a 4th column containing "Maximum Tenure" against each id such that:

ID Year Tenure MaxTenure
12 1992 1 4
12 1993 2 4
12 1994 3 4
12 1995 4 4
225 2001 1 6
225 2002 2 6
225 2003 3 6
225 2004 4 6
225 2005 5 6
225 2006 6 6
3045 2009 1 3
3045 2010 2 3
3045 2011 3 3

How can I do it? Please help.

Subject: Conditional Maximum

From: dpb

Date: 24 Nov, 2012 19:13:11

Message: 2 of 3

On 11/24/2012 12:07 PM, Rahat wrote:
> I have 3 columns in my database:
>
> ID Year Tenure
> 12 1992 1
.
> 12 1995 4
> 225 2001 1
...
> 225 2006 6
> 3045 2009 1
...

> I want to keep IDs with a tenure >=5 years. For that matter I want to
> create a 4th column containing "Maximum Tenure" against each id such that:
>
> ID Year Tenure MaxTenure
> 12 1992 1 4
> 12 1993 2 4
> 12 1994 3 4
> 12 1995 4 4
> 225 2001 1 6
...

ix=[diff([x(:,1)])>1 ;true];
z=[];for i=1:length(maxT),z=[z;repmat(maxT(i),maxT(i),1)];end
x(:,4)=z;

There's got to be a more clever way to do the expansion--I've got brain
cramp at the moment, though, apparently...

--

Subject: Conditional Maximum

From: Roger Stafford

Date: 24 Nov, 2012 20:54:08

Message: 3 of 3

"Rahat" wrote in message <k8r2ca$1ie$1@newscl01ah.mathworks.com>...
> I have 3 columns in my database:
>
> ID Year Tenure
> 12 1992 1
> 12 1993 2
> 12 1994 3
> 12 1995 4
> 225 2001 1
> 225 2002 2
> 225 2003 3
> 225 2004 4
> 225 2005 5
> 225 2006 6
> 3045 2009 1
> 3045 2010 2
> 3045 2011 3
>
> I want to keep IDs with a tenure >=5 years. For that matter I want to create a 4th column containing "Maximum Tenure" against each id such that:
>
> ID Year Tenure MaxTenure
> 12 1992 1 4
> 12 1993 2 4
> 12 1994 3 4
> 12 1995 4 4
> 225 2001 1 6
> 225 2002 2 6
> 225 2003 3 6
> 225 2004 4 6
> 225 2005 5 6
> 225 2006 6 6
> 3045 2009 1 3
> 3045 2010 2 3
> 3045 2011 3 3
>
> How can I do it? Please help.
- - - - - - - - - -
  Let A be the array of your database.

 A = sortrows(A,[1,3]); % <-- This is in case A is not sorted as in your example
 [~,m,n] = unique(A(:,1),'last');
 A2 = [A,A(m(n),3)]; % <-- This is the new database

  Your request "I want to keep IDs with a tenure >=5 years" seems totally incompatible with your second request. The above is only for the second request.

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