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:
insert new value to matrix

Subject: insert new value to matrix

From: edward kabanyas

Date: 15 May, 2012 09:36:06

Message: 1 of 3

Dear all;

Suppose we have a matrix A as:

A =
[1 11 1 8 100 100
1 11 1 8 100 100
1 11 1 9 100 100
1 11 1 9 100 100
1 11 1 12 100 100
1 11 1 12 100 100
1 11 1 13 100 100
1 11 1 13 100 100];

3th and 4th column are day and hour, respectively. The data from the same hour are repeated every 2 rows . I want to make hour/time in sequence, for which no sequence I want to add new data such as 0. From the above case, it becomes:

Anew =
[1 11 1 8 100 100
1 11 1 8 100 100
1 11 1 9 100 100
1 11 1 9 100 100
1 11 1 10 0 0
1 11 1 10 0 0
1 11 1 11 0 0
1 11 1 11 0 0
1 11 1 12 100 100
1 11 1 12 100 100
1 11 1 13 100 100
1 11 1 13 100 100];

Another example

A=
[1 11 1 23 100 100
1 11 1 23 100 100
1 11 2 1 100 100
1 11 2 1 100 100];

This must be:
Anew=
[1 11 1 23 100 100
1 11 1 23 100 100
1 11 2 0 0 0
1 11 2 0 0 0
1 11 2 1 100 100
1 11 2 1 100 100]

For now, I am thinking to use looping procedure, by checking every 3 rows of 3th and 4th column. Probably you have another option/suggestion. Thanks for sharing..

Best regards,

Edward

 

Subject: insert new value to matrix

From: Roger Stafford

Date: 16 May, 2012 00:02:07

Message: 2 of 3

"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <jot826$889$1@newscl01ah.mathworks.com>...
> Dear all;
>
> Suppose we have a matrix A as:
>
> A =
> [1 11 1 8 100 100
> 1 11 1 8 100 100
> 1 11 1 9 100 100
> 1 11 1 9 100 100
> 1 11 1 12 100 100
> 1 11 1 12 100 100
> 1 11 1 13 100 100
> 1 11 1 13 100 100];
>
> 3th and 4th column are day and hour, respectively. The data from the same hour are repeated every 2 rows . I want to make hour/time in sequence, for which no sequence I want to add new data such as 0. From the above case, it becomes:
>
> Anew =
> [1 11 1 8 100 100
> 1 11 1 8 100 100
> 1 11 1 9 100 100
> 1 11 1 9 100 100
> 1 11 1 10 0 0
> 1 11 1 10 0 0
> 1 11 1 11 0 0
> 1 11 1 11 0 0
> 1 11 1 12 100 100
> 1 11 1 12 100 100
> 1 11 1 13 100 100
> 1 11 1 13 100 100];
>
> Another example
>
> A=
> [1 11 1 23 100 100
> 1 11 1 23 100 100
> 1 11 2 1 100 100
> 1 11 2 1 100 100];
>
> This must be:
> Anew=
> [1 11 1 23 100 100
> 1 11 1 23 100 100
> 1 11 2 0 0 0
> 1 11 2 0 0 0
> 1 11 2 1 100 100
> 1 11 2 1 100 100]
>
> For now, I am thinking to use looping procedure, by checking every 3 rows of 3th and 4th column. Probably you have another option/suggestion. Thanks for sharing..
>
> Best regards,
>
> Edward
- - - - - - - - -
 t0 = A(1,3)*24+A(1,4);
 t1 = 2*(A(:,3)*24+A(:,4)-t0)+mod((0:size(A,1)-1).',2)+1;
 n = t1(end);
 Anew = repmat([A(1,[1 2]),0,0,0,0],n,1);
 Anew(t1,:) = A;
 t1 = floor((2*t0:2*t0+n-1).'/2);
 t2 = mod(t1,24);
 t1 = (t1-t2)/24;
 Anew(:,[3,4]) = [t1,t2];

Roger Stafford

Subject: insert new value to matrix

From: edward kabanyas

Date: 16 May, 2012 04:21:06

Message: 3 of 3

Roger,

Thanks for nice suggestion. I have used looping procedure so far, but for sure I will use your code to make the code shorter and faster.

Thanks again,
EDward



"Roger Stafford" wrote in message <jouqpv$n5u$1@newscl01ah.mathworks.com>...
> "edward kabanyas" <djuky_hmi@yahoo.com> wrote in message <jot826$889$1@newscl01ah.mathworks.com>...
> > Dear all;
> >
> > Suppose we have a matrix A as:
> >
> > A =
> > [1 11 1 8 100 100
> > 1 11 1 8 100 100
> > 1 11 1 9 100 100
> > 1 11 1 9 100 100
> > 1 11 1 12 100 100
> > 1 11 1 12 100 100
> > 1 11 1 13 100 100
> > 1 11 1 13 100 100];
> >
> > 3th and 4th column are day and hour, respectively. The data from the same hour are repeated every 2 rows . I want to make hour/time in sequence, for which no sequence I want to add new data such as 0. From the above case, it becomes:
> >
> > Anew =
> > [1 11 1 8 100 100
> > 1 11 1 8 100 100
> > 1 11 1 9 100 100
> > 1 11 1 9 100 100
> > 1 11 1 10 0 0
> > 1 11 1 10 0 0
> > 1 11 1 11 0 0
> > 1 11 1 11 0 0
> > 1 11 1 12 100 100
> > 1 11 1 12 100 100
> > 1 11 1 13 100 100
> > 1 11 1 13 100 100];
> >
> > Another example
> >
> > A=
> > [1 11 1 23 100 100
> > 1 11 1 23 100 100
> > 1 11 2 1 100 100
> > 1 11 2 1 100 100];
> >
> > This must be:
> > Anew=
> > [1 11 1 23 100 100
> > 1 11 1 23 100 100
> > 1 11 2 0 0 0
> > 1 11 2 0 0 0
> > 1 11 2 1 100 100
> > 1 11 2 1 100 100]
> >
> > For now, I am thinking to use looping procedure, by checking every 3 rows of 3th and 4th column. Probably you have another option/suggestion. Thanks for sharing..
> >
> > Best regards,
> >
> > Edward
> - - - - - - - - -
> t0 = A(1,3)*24+A(1,4);
> t1 = 2*(A(:,3)*24+A(:,4)-t0)+mod((0:size(A,1)-1).',2)+1;
> n = t1(end);
> Anew = repmat([A(1,[1 2]),0,0,0,0],n,1);
> Anew(t1,:) = A;
> t1 = floor((2*t0:2*t0+n-1).'/2);
> t2 = mod(t1,24);
> t1 = (t1-t2)/24;
> Anew(:,[3,4]) = [t1,t2];
>
> Roger Stafford

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