Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Separating numbers in a matrix
Replies: 6   Last Post: May 23, 2012 12:49 PM

 Messages: [ Previous | Next ]
 Galam Lee Posts: 14 Registered: 3/28/12
Re: Separating numbers in a matrix
Posted: May 23, 2012 5:04 AM

"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

It runs well!

Date Subject Author
5/22/12 Galam Lee
5/22/12 ImageAnalyst
5/22/12 Nasser Abbasi
5/23/12 Galam Lee
5/22/12 Roger Stafford
5/23/12 Galam Lee
5/23/12 ImageAnalyst