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:
Forming a new matrix by taking particular elements of an existing matrix

Subject: Forming a new matrix by taking particular elements of an existing matrix

From: Kenan

Date: 23 Dec, 2011 11:22:09

Message: 1 of 5

Hello to all,

I have a 6x5 matrix as below:

     0 0 0 0 0
     0 900 1000 1100 0
     0 600 700 800 0
     0 300 400 500 0
     0 100 150 200 0
     0 0 0 0 0

and I have to take its non-zero elements and form a 12x1 column matrix. Also i want tto take elements of the new matrix to start from the bottom left of the above matrix on. That is,

100
150
200
300
400
500
...
1100

How can i do this? All help will be appreciated. Thanks in advance.
Kenan

Subject: Forming a new matrix by taking particular elements of an existing

From: Nasser M. Abbasi

Date: 23 Dec, 2011 13:28:58

Message: 2 of 5

On 12/23/2011 5:22 AM, Kenan wrote:
> Hello to all,
>
> I have a 6x5 matrix as below:
>
> 0 0 0 0 0
> 0 900 1000 1100 0
> 0 600 700 800 0
> 0 300 400 500 0
> 0 100 150 200 0
> 0 0 0 0 0
>
> and I have to take its non-zero elements and form a 12x1 column matrix. Also i want tto
>take elements of the new matrix to start from the bottom left of the above matrix on. That is,
>
> 100
> 150
> 200
> 300
> 400
> 500
> ...
> 1100
>
> How can i do this? All help will be appreciated. Thanks in advance.
> Kenan

Not sure if I understood the rest of the question. but may be

EDU>> sort(nonzeros(A))

ans =

          100
          150
          200
          300
          400
          500
          600
          700
          800
          900
         1000
         1100

Where A is your matrix above.

--Nasser

Subject: Forming a new matrix by taking particular elements of an existing matrix

From: Roger Stafford

Date: 23 Dec, 2011 19:11:08

Message: 3 of 5

"Kenan" wrote in message <jd1o90$flt$1@newscl01ah.mathworks.com>...
> and I have to take its non-zero elements and form a 12x1 column matrix. Also i want tto take elements of the new matrix to start from the bottom left of the above matrix on.- - - - - - - - -
  Based on your statement "start from the bottom left of the above matrix on" perhaps you mean the following. With A your original matrix:

  B = reshape(flipud(A).',[],1);
  B = B(B~=0);

Roger Stafford

Subject: Forming a new matrix by taking particular elements of an existing matrix

From: Kenan

Date: 23 Dec, 2011 22:19:08

Message: 4 of 5

Dear Nasser and Roger, thank you for your answers.

"Roger Stafford" wrote in message <jd2joc$3uf$1@newscl01ah.mathworks.com>...
> "Kenan" wrote in message <jd1o90$flt$1@newscl01ah.mathworks.com>...
> > and I have to take its non-zero elements and form a 12x1 column matrix. Also i want tto take elements of the new matrix to start from the bottom left of the above matrix on.- - - - - - - - -
> Based on your statement "start from the bottom left of the above matrix on" perhaps you mean the following. With A your original matrix:
>
> B = reshape(flipud(A).',[],1);
> B = B(B~=0);
>
> Roger Stafford

Yes, that was what i meant exactly. Thank you again.

Subject: Forming a new matrix by taking particular elements of an existing matrix

From: Matt J

Date: 25 Dec, 2011 00:45:10

Message: 5 of 5

"Kenan" wrote in message <jd2uos$52o$1@newscl01ah.mathworks.com>...
>
> > Based on your statement "start from the bottom left of the above matrix on" perhaps you mean the following. With A your original matrix:
> >
> > B = reshape(flipud(A).',[],1);
> > B = B(B~=0);
> >
> > Roger Stafford
>
> Yes, that was what i meant exactly. Thank you again.
==============


But as a small point of optimization, note that

B=nonzeros(B)

is better (more memory-efficient) than

B = B(B~=0)

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