Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Galam Lee

Posts: 12
Registered: 3/28/12
Re: Separating numbers in a matrix
Posted: May 23, 2012 5:04 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"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!



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.