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 regular matrix from an irregular matrix

Subject: creating a regular matrix from an irregular matrix

From: Liviu Parpalea

Date: 27 Mar, 2009 20:05:17

Message: 1 of 7

hello,

i'm trying to create a regular matrix of 9 by 62 from a vector of 541 values. the 451 values are split into groups that have the same values and i used the "unique" function to find out how many such groups i have, and it turns out that there are 62 groups.
the problem is that the number of values in each group rages from 1 to 9, and when i try to use the "find" command it gives me an error, since i'm trying to create an irregular matrix. my question is how do i fill in the blank spaces in my irregular matrix with either NaN (idealy) or 0 so that i end up with a 9 by 62 matrix.

thanks alot for any help anyone can give

Subject: creating a regular matrix from an irregular matrix

From: someone

Date: 27 Mar, 2009 20:28:01

Message: 2 of 7

"Liviu Parpalea" <liviu142000@yahoo.ca> wrote in message <gqjbht$cn0$1@fred.mathworks.com>...
> hello,
>
> i'm trying to create a regular matrix of 9 by 62 from a vector of 541 values. the 451 values are split into groups that have the same values and i used the "unique" function to find out how many such groups i have, and it turns out that there are 62 groups.
> the problem is that the number of values in each group rages from 1 to 9, and when i try to use the "find" command it gives me an error, since i'm trying to create an irregular matrix. my question is how do i fill in the blank spaces in my irregular matrix with either NaN (idealy) or 0 so that i end up with a 9 by 62 matrix.
>
> thanks alot for any help anyone can give

% I'm not exactly sure how you want to fill the new matrix,
% but you can try something like:

new_matrix = repmat(NaN,9,62); % preallocate

% and if v is your vector, then

new_matrix(1:length(v)) = v; % use linear indexing

% I'm sure there are other (better) solutions
% depending on your needs.

Subject: creating a regular matrix from an irregular matrix

From: Roger Stafford

Date: 27 Mar, 2009 22:16:01

Message: 3 of 7

"Liviu Parpalea" <liviu142000@yahoo.ca> wrote in message <gqjbht$cn0$1@fred.mathworks.com>...
> hello,
>
> i'm trying to create a regular matrix of 9 by 62 from a vector of 541 values. the 451 values are split into groups that have the same values and i used the "unique" function to find out how many such groups i have, and it turns out that there are 62 groups.
> the problem is that the number of values in each group rages from 1 to 9, and when i try to use the "find" command it gives me an error, since i'm trying to create an irregular matrix. my question is how do i fill in the blank spaces in my irregular matrix with either NaN (idealy) or 0 so that i end up with a 9 by 62 matrix.
>
> thanks alot for any help anyone can give

  Call your original vector v. If v is a column vector, do this:

 u = sort(v);
 t = [true;diff(u)~=0];
 p = cumsum(t);
 q = ones(1,size(t,1));
 f = find(t);
 q(f(2:end)) = f(1:end-1)-f(2:end)+1;
 q = cumsum(q);
 M = accumarray(p,q,u));

The unused spaces in M will be zeros. This works unless some of the elements in v are also zero.

Roger Stafford

Subject: creating a regular matrix from an irregular matrix

From: Roger Stafford

Date: 27 Mar, 2009 22:29:01

Message: 4 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqjj71$4r9$1@fred.mathworks.com>...
> .....
> M = accumarray(p,q,u));
> .....

  Oops! I meant to write:

 M = accumarray(p,q,u);

for the last line.

Roger Stafford

Subject: creating a regular matrix from an irregular matrix

From: Roger Stafford

Date: 28 Mar, 2009 14:13:01

Message: 5 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqjjvd$omv$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqjj71$4r9$1@fred.mathworks.com>...
> > .....
> > M = accumarray(p,q,u));
> > .....
>
> Oops! I meant to write:
>
> M = accumarray(p,q,u);
>
> for the last line.
>
> Roger Stafford

  A second oops for that last line (blush). It should read:

 M = accumarray([p,q],u);

Roger Stafford

Subject: creating a regular matrix from an irregular matrix

From: Bruno Luong

Date: 28 Mar, 2009 14:27:02

Message: 6 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gqlb9d$j1f$1@fred.mathworks.com>...

>
> A second oops for that last line (blush). It should read:
>
> M = accumarray([p,q],u);
>
> Roger Stafford

Also q should be initialized in column:

q = ones(size(t,1),1)

Bruno

Subject: creating a regular matrix from an irregular matrix

From: Roger Stafford

Date: 28 Mar, 2009 14:46:01

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gqlc3m$bug$1@fred.mathworks.com>...
> Also q should be initialized in column:
>
> q = ones(size(t,1),1)
>
> Bruno

  Yes, you're right, Bruno. Thanks.

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