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:
creating a matrix from an existing matrix

Subject: creating a matrix from an existing matrix

From: MOOON MOOON

Date: 12 May, 2010 16:14:05

Message: 1 of 10


Hello,

I want to know the method of creating a matrix

from an existing matrix depending on certain

specifications made by the user.

for example,

assume we have the matrix:

x=[2 3 5 9 2 3 6 8 1 0 6 9]

I want to create a new matrix that takes

only the two elements then ignore the third

one and then take the next two elements and

ignore the third and so on.

So, depending on the above matrix,

the new matrix is:

y=[2 3 9 2 6 8 0 6]

Also, another example is making a new matrix

that takes the entries that have odd sequence

i.e entries number 1,3,5,7,9,11

which are in the above example: [ 2 5 2 6 1 6]

or , in general, any specifications or rules made by

the user.

Could you please help me to solve

this problem.

Thanks..

Subject: creating a matrix from an existing matrix

From: James Tursa

Date: 12 May, 2010 16:27:04

Message: 2 of 10

"MOOON MOOON" <shaheed107@yahoo.com> wrote in message <hsek4c$qng$1@fred.mathworks.com>...
>
> Hello,
>
> I want to know the method of creating a matrix
>
> from an existing matrix depending on certain
>
> specifications made by the user.
>
> for example,
>
> assume we have the matrix:
>
> x=[2 3 5 9 2 3 6 8 1 0 6 9]
>
> I want to create a new matrix that takes
>
> only the two elements then ignore the third
>
> one and then take the next two elements and
>
> ignore the third and so on.
>
> So, depending on the above matrix,
>
> the new matrix is:
>
> y=[2 3 9 2 6 8 0 6]
>
> Also, another example is making a new matrix
>
> that takes the entries that have odd sequence
>
> i.e entries number 1,3,5,7,9,11
>
> which are in the above example: [ 2 5 2 6 1 6]
>
> or , in general, any specifications or rules made by
>
> the user.
>
> Could you please help me to solve
>
> this problem.
>
> Thanks..

To lop off every 3rd value, assuming that the total number of elements is a multiple of three:

A = rand(3*n);
A = reshape(A,3,[]);
A = A(1:2,:);
A = reshape(A,1,[]);

To lop off every other value, assuming that the total number of elements is a multiple of two:

A = rand(2*n);
A = reshape(A,2,[]);
A = A(1,:);

Basically, consider how the values are laid out in memory, then a reshape and array slice operation will often suffice.

James Tursa

Subject: creating a matrix from an existing matrix

From: Matt J

Date: 12 May, 2010 16:29:24

Message: 3 of 10

"MOOON MOOON" <shaheed107@yahoo.com> wrote in message <hsek4c$qng$1@fred.mathworks.com>...

> assume we have the matrix:
>
> x=[2 3 5 9 2 3 6 8 1 0 6 9]
>
> I want to create a new matrix that takes
>
> only the two elements then ignore the third
>
> one and then take the next two elements and
>
> ignore the third and so on.
>
> So, depending on the above matrix,
>
> the new matrix is:
>
> y=[2 3 9 2 6 8 0 6]
>
=================


y=x;
y(3:3:end)=[], %or in your second example y(2:2:end)=[]

Subject: creating a matrix from an existing matrix

From: Matt Fig

Date: 12 May, 2010 16:40:24

Message: 4 of 10

x=[2 3 5 9 2 3 6 8 1 0 6 9];
x(3:3:end) = [] % Pruning technique


x=[2 3 5 9 2 3 6 8 1 0 6 9];
x = x(1:2:end) % Selecting technique

Subject: creating a matrix from an existing matrix

From: MOOON MOOON

Date: 12 May, 2010 21:58:03

Message: 5 of 10



Ok

very great

that works very good

I want to thank everyone who gave me this help

thanks

thanks

thanks

Subject: creating a matrix from an existing matrix

From: MOOON MOOON

Date: 13 May, 2010 02:37:04

Message: 6 of 10


Ok,

thanks ..

this is well understood, but there is a question that came into my mind

and this problem is the same problem that I have in my research.

I have a matrix containing the power demand for every day in the whole

year, so we have 365 points as follows:

P=[4 3 5 2 3 6 ........3 2 7]

So, the value ( 4 ) is the first value of January, then 3, then 5

and continues for the remaining months until the value ( 7 )

which is the value number 31 of December.

Actually, the example given in previous post was in

systematic matrix and the application was made in sequence to the matrix elements.

But,I want the new matrix P to include the first 20 elements of each

month and ignore the remaining days which are 8 or 9 or 10 or 11 depending

on the month.

AND THIS IS THE PROBLEM. THE NUMBER OF DAYS IS DIFFERENT FOR EACH MONTH

The problem is: the days of the months are different and we cannot

go in systematic way as in the previous examples.

In reality, if all months have equal number of days, we will not

face any problem applying the previous methods.

we have for January:31 days

February: 28 days, March : 31 days and so on

How can we proceed in this matter.

I am sorry for disturb you, but this matter is very urgent for me

since I am currently doing my master degree thesis work.

Thanks

Subject: creating a matrix from an existing matrix

From: ImageAnalyst

Date: 13 May, 2010 02:51:21

Message: 7 of 10

You know what day of the year each month starts on and what day of the
year the 20th of the month is. It's the same every year, with the
exception of leap years but that's easy to handle. So just extract
them, for example

P2 = [P(1:20), P(32:51), and so on];

Is that not right -- am I overlooking something? Seems rather trivial
to me.

Subject: creating a matrix from an existing matrix

From: MOOON MOOON

Date: 13 May, 2010 03:03:05

Message: 8 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <cc3d68a4-cbe5-4fc2-9aaf-857b343a7a5c@i10g2000yqh.googlegroups.com>...
> You know what day of the year each month starts on and what day of the
> year the 20th of the month is. It's the same every year, with the
> exception of leap years but that's easy to handle. So just extract
> them, for example
>
> P2 = [P(1:20), P(32:51), and so on];
>
> Is that not right -- am I overlooking something? Seems rather trivial
> to me.


Oh,

execuse me, I do not have a good background about matlab.

Thanks, I understand the matter perfectly

thank you for your help

Subject: creating a matrix from an existing matrix

From: TideMan

Date: 13 May, 2010 03:11:00

Message: 9 of 10

On May 13, 2:37 pm, "MOOON MOOON" <shaheed...@yahoo.com> wrote:
> Ok,
>
> thanks ..
>
> this is well understood, but there is a question that came into my mind
>
> and this problem is the same problem that I have in my research.
>
> I have a matrix containing the power demand for every day in the whole
>
> year, so we have 365 points as follows:
>
> P=[4 3 5 2 3 6 ........3 2 7]
>
> So, the value ( 4 ) is the first value of January, then 3, then 5
>
> and continues for the remaining months until the value ( 7 )
>
> which is the value number 31 of December.
>
> Actually, the example given in previous post was in
>
> systematic matrix and the application was made in sequence to the matrix elements.
>
> But,I want the new matrix P to include the first 20 elements of each
>
> month and ignore the remaining days which are 8 or 9 or 10 or 11 depending
>
> on the month.
>
> AND THIS IS THE PROBLEM. THE NUMBER OF DAYS IS DIFFERENT FOR EACH MONTH
>
> The problem is: the days of the months are different and we cannot
>
> go in systematic way as in the previous examples.
>
> In reality, if all months have equal number of days, we will not
>
> face any problem applying the previous methods.
>
> we have for January:31 days
>
> February: 28 days, March : 31 days    and so on
>
> How can we proceed in this matter.
>
> I am sorry for disturb you, but this matter is very urgent for me
>
> since I am currently doing my master degree thesis work.
>
> Thanks

Here's one way:
yr=2010;
t1=datenum(yr,1,1); % 1-Jan-yyyy
t2=datenum(yr+1,1,1)-1; % 31-Dec-yyyy
t=[t1:t2]'; % Days of the year in Matlab days
date=str2num(datestr(t,'yyyymmdd'));
indx=find(mod(date,100) <= 20);

P(indx) are your data.

Subject: creating a matrix from an existing matrix

From: Matt Fig

Date: 13 May, 2010 03:28:04

Message: 10 of 10

Another method:

% Data
D = round(rand(1,365)*6);
N = 20; % Keep this many days of each month.

% Engine
EM = eomday(2010,1:12); % See the help.
S = [1 cumsum(EM(1:11))+1];
E = S + (N-1);
SE = cumsum(E - S + 1);
idx = ones(1,SE(12));
idx(1+SE(1:11)) = S(2:12) - E(1:11);
D2 = D(cumsum(idx)); % The new data set.

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