Got Questions? Get Answers.
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:
2D array to 1D array

Subject: 2D array to 1D array

From: Brianne

Date: 19 Apr, 2011 00:16:04

Message: 1 of 3

I have a matrix that is 9 x 52. Nine represents the maximum number of circumferential locations at a specificed longitudinal location around a model and 52 represents the longitudinal location along the model. For example, at longitudinal station #1 there are 4 circumferential values. At station #5 there are 8 circumferential values. However, MATLAB pads in zeros to make the matrix a square.

My question is how to I grab those specific values out of the 2D array and place it into a 1D array? The reshape function will not work because the matrix dimension will be changing.

For the purpose of my question I will use a simpler example. Assume A is a 6 x 6 matrix. For columns 1 through 4, I want to put the values of rows 1 through 4 into a 1D array. Then append column 5 and rows 1 through 5 to the 1D array. Then append column 6 and rows 1 through 6 to the 1D array. Finally the 1D array would look like B.

A =[
     0 0 0 0 1 1
     0 0 0 0 1 1
     0 0 0 0 1 1
     0 0 0 0 1 1
     0 0 0 0 1 1
     0 0 0 0 0 1];

B = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1];

I would like this process to be automated because of the size of the matrix and the real matrix is not in any specific order. For example, columns 1 through 4 contain 4 of the necessary rows, but columsn 5, 6, 10 contain 8 of the necessary rows. I know how many rows I should take for each column. Also I can not use a sparse function to remove the padding because there are values that are zero and actually mean something. While other values are zeros and is just there for padding.

Thank you in advanced.

Subject: 2D array to 1D array

From: Roger Stafford

Date: 19 Apr, 2011 03:26:04

Message: 2 of 3

"Brianne " <brianne.y.williams@aero.org> wrote in message <ioik84$mnc$1@fred.mathworks.com>...
> I have a matrix that is 9 x 52. Nine represents the maximum number of circumferential locations at a specificed longitudinal location around a model and 52 represents the longitudinal location along the model. For example, at longitudinal station #1 there are 4 circumferential values. At station #5 there are 8 circumferential values. However, MATLAB pads in zeros to make the matrix a square.
>
> My question is how to I grab those specific values out of the 2D array and place it into a 1D array? The reshape function will not work because the matrix dimension will be changing.
>
> For the purpose of my question I will use a simpler example. Assume A is a 6 x 6 matrix. For columns 1 through 4, I want to put the values of rows 1 through 4 into a 1D array. Then append column 5 and rows 1 through 5 to the 1D array. Then append column 6 and rows 1 through 6 to the 1D array. Finally the 1D array would look like B.
>
> A =[
> 0 0 0 0 1 1
> 0 0 0 0 1 1
> 0 0 0 0 1 1
> 0 0 0 0 1 1
> 0 0 0 0 1 1
> 0 0 0 0 0 1];
>
> B = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1];
>
> I would like this process to be automated because of the size of the matrix and the real matrix is not in any specific order. For example, columns 1 through 4 contain 4 of the necessary rows, but columsn 5, 6, 10 contain 8 of the necessary rows. I know how many rows I should take for each column. Also I can not use a sparse function to remove the padding because there are values that are zero and actually mean something. While other values are zeros and is just there for padding.
>
> Thank you in advanced.
- - - - - - - - -
  If 'r' is a row vector giving the desired number of "circumferential locations" at each respective "longitudinal location", then do:

 B = A(bsxfun(@minus,r,(1:size(A,1)).')>=0).';

Roger Stafford

Subject: 2D array to 1D array

From: Brianne

Date: 19 Apr, 2011 19:37:04

Message: 3 of 3

Thanks a lot!!! It worked.

"Roger Stafford" wrote in message <ioivcc$h56$1@fred.mathworks.com>...
> "Brianne " <brianne.y.williams@aero.org> wrote in message <ioik84$mnc$1@fred.mathworks.com>...
> > I have a matrix that is 9 x 52. Nine represents the maximum number of circumferential locations at a specificed longitudinal location around a model and 52 represents the longitudinal location along the model. For example, at longitudinal station #1 there are 4 circumferential values. At station #5 there are 8 circumferential values. However, MATLAB pads in zeros to make the matrix a square.
> >
> > My question is how to I grab those specific values out of the 2D array and place it into a 1D array? The reshape function will not work because the matrix dimension will be changing.
> >
> > For the purpose of my question I will use a simpler example. Assume A is a 6 x 6 matrix. For columns 1 through 4, I want to put the values of rows 1 through 4 into a 1D array. Then append column 5 and rows 1 through 5 to the 1D array. Then append column 6 and rows 1 through 6 to the 1D array. Finally the 1D array would look like B.
> >
> > A =[
> > 0 0 0 0 1 1
> > 0 0 0 0 1 1
> > 0 0 0 0 1 1
> > 0 0 0 0 1 1
> > 0 0 0 0 1 1
> > 0 0 0 0 0 1];
> >
> > B = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1];
> >
> > I would like this process to be automated because of the size of the matrix and the real matrix is not in any specific order. For example, columns 1 through 4 contain 4 of the necessary rows, but columsn 5, 6, 10 contain 8 of the necessary rows. I know how many rows I should take for each column. Also I can not use a sparse function to remove the padding because there are values that are zero and actually mean something. While other values are zeros and is just there for padding.
> >
> > Thank you in advanced.
> - - - - - - - - -
> If 'r' is a row vector giving the desired number of "circumferential locations" at each respective "longitudinal location", then do:
>
> B = A(bsxfun(@minus,r,(1:size(A,1)).')>=0).';
>
> 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