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:
Separating numbers in a matrix

Subject: Separating numbers in a matrix

From: Galam Lee

Date: 23 May, 2012 01:39:07

Message: 1 of 7

Hello all-

I have a matrix as below.
A=[45 50; 200 205; 355 433]

I would like to separate the numbers from the matrix and make two new matrixes which contain 2-digit numbers.
eg) A1=[45 50 ;00 05; 55 33]
      A2=[00 00; 02 02; 03 04]

How can I do this?

Kind regards,
Galam Lee

Subject: Separating numbers in a matrix

From: Nasser M. Abbasi

Date: 23 May, 2012 02:26:46

Message: 2 of 7

On 5/22/2012 8:39 PM, Galam Lee wrote:
> Hello all-
>
> I have a matrix as below.
> A=[45 50; 200 205; 355 433]
>
> I would like to separate the numbers from the matrix and make
>two new matrixes which contain 2-digit numbers.
> eg) A1=[45 50 ;00 05; 55 33]
> A2=[00 00; 02 02; 03 04]
>
> How can I do this?
>
> Kind regards,
> Galam Lee

I am lazy now. Here is my brain dead solution:

----------------------------------------------
A=[45 50; 200 205; 355 433];

A1=zeros(size(A));
A2=zeros(size(A));

for i = 1:size(A,1)
     for j = 1:size(A,2)
         
         x = num2str(A(i,j));
         N = length(x);
         if N>2
            A2(i,j) = str2double(x(1:N-2));
            A1(i,j) = str2double(x(N-1:N));
         else
            A1(i,j) = str2double(x);
         end
         
     end
end

A1
A2
--------------------------

A1 =

     45 50
      0 5
     55 33


A2 =

      0 0
      2 2
      3 4


I am sure there is a much smarter way of doing it.

--Nasser

Subject: Separating numbers in a matrix

From: Roger Stafford

Date: 23 May, 2012 02:51:06

Message: 3 of 7

"Galam Lee" wrote in message <jphf3r$9no$1@newscl01ah.mathworks.com>...
> A=[45 50; 200 205; 355 433]
>
> I would like to separate the numbers from the matrix and make two new matrixes which contain 2-digit numbers.
> eg) A1=[45 50 ;00 05; 55 33]
> A2=[00 00; 02 02; 03 04]
- - - - - - - - - - -
  Are these 'double' quantities or strings? If they are double, do this:

 A1 = mod(A,100);
 A2 = (A-A1)/100;

  I don't know how you want to handle A if it contains negative numbers or non-integers.

Roger Stafford

Subject: Separating numbers in a matrix

From: Galam Lee

Date: 23 May, 2012 09:04:07

Message: 4 of 7

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jphht6$5r3$1@speranza.aioe.org>...
> On 5/22/2012 8:39 PM, Galam Lee wrote:
> > Hello all-
> >
> > I have a matrix as below.
> > A=[45 50; 200 205; 355 433]
> >
> > I would like to separate the numbers from the matrix and make
> >two new matrixes which contain 2-digit numbers.
> > eg) A1=[45 50 ;00 05; 55 33]
> > A2=[00 00; 02 02; 03 04]
> >
> > How can I do this?
> >
> > Kind regards,
> > Galam Lee
>
> I am lazy now. Here is my brain dead solution:
>
> ----------------------------------------------
> A=[45 50; 200 205; 355 433];
>
> A1=zeros(size(A));
> A2=zeros(size(A));
>
> for i = 1:size(A,1)
> for j = 1:size(A,2)
>
> x = num2str(A(i,j));
> N = length(x);
> if N>2
> A2(i,j) = str2double(x(1:N-2));
> A1(i,j) = str2double(x(N-1:N));
> else
> A1(i,j) = str2double(x);
> end
>
> end
> end
>
> A1
> A2
> --------------------------
>
> A1 =
>
> 45 50
> 0 5
> 55 33
>
>
> A2 =
>
> 0 0
> 2 2
> 3 4
>
>
> I am sure there is a much smarter way of doing it.
>
> --Nasser

Thank you for your reply :)
It runs well!

Subject: Separating numbers in a matrix

From: Galam Lee

Date: 23 May, 2012 09:09:07

Message: 5 of 7

"Roger Stafford" wrote in message <jphjaq$qf3$1@newscl01ah.mathworks.com>...
> "Galam Lee" wrote in message <jphf3r$9no$1@newscl01ah.mathworks.com>...
> > A=[45 50; 200 205; 355 433]
> >
> > I would like to separate the numbers from the matrix and make two new matrixes which contain 2-digit numbers.
> > eg) A1=[45 50 ;00 05; 55 33]
> > A2=[00 00; 02 02; 03 04]
> - - - - - - - - - - -
> Are these 'double' quantities or strings? If they are double, do this:
>
> A1 = mod(A,100);
> A2 = (A-A1)/100;
>
> I don't know how you want to handle A if it contains negative numbers or non-integers.
>
> Roger Stafford

Thank you for your reply :)
It's so simple method.
Also, matrix A does not contain negative and non-integer numbers, so it's not problem.

Subject: Separating numbers in a matrix

From: Image Analyst

Date: 23 May, 2012 16:49:06

Message: 6 of 7

"Roger Stafford" wrote in message <jphjaq$qf3$1@newscl01ah.mathworks.com>...
> A1 = mod(A,100);
> A2 = (A-A1)/100;
> Roger Stafford
-------------------------------------------------------------
Same as my answer that I posted in Google. Not sure why things aren't being transferred from Google to Mathworks - somebody's dropping the ball.

Subject: Separating numbers in a matrix

From: ImageAnalyst

Date: 23 May, 2012 02:05:15

Message: 7 of 7

On May 22, 9:39

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