How to retrieve values from cell array of character type?

  • Follow


Hello,
I'm using MAT-LAB programming for my project work, and for the same I have to read columns of the various sequences simultaneously in a single array. For that I used the 'fastaread' function and I got my sequences in a matrix as cell array like this

Seq=[4x1 char]

And when I'm reading this using 'for' loop I'm getting values like this,

S=[1x439 char]
    [1x404 char]
    [1x404 char]
    [1x343 char]

 But instead I want the columns of the sequences used, as like
s1=M
     P
     I
     L

As, I want to use only the columns for my further calculations.
So, Please if anyone can look into my problem.

Thank you so much...!
0
Reply Priyanka 7/26/2010 11:36:05 AM

"Priyanka Choudhary" <manipriyanka11@gmail.com> wrote in message <i2jrv5$lsa$1@fred.mathworks.com>...
> Hello,
> I'm using MAT-LAB programming for my project work, and for the same I have to read columns of the various sequences simultaneously in a single array. For that I used the 'fastaread' function and I got my sequences in a matrix as cell array like this
> 
> Seq=[4x1 char]
> 
> And when I'm reading this using 'for' loop I'm getting values like this,
> 
> S=[1x439 char]
>     [1x404 char]
>     [1x404 char]
>     [1x343 char]
> 
>  But instead I want the columns of the sequences used, as like
> s1=M
>      P
>      I
>      L
> 
> As, I want to use only the columns for my further calculations.
> So, Please if anyone can look into my problem.
> 
> Thank you so much...!

one of the solutions

     s={
          'abc'
          'de'
          'fghi'
     };
     ix=2;     % <- get second col...
     r=cellfun(@(x) x(ix),s)
%{
%    r =
     b
     e
     g
%}

us
0
Reply us 7/26/2010 11:47:05 AM


Priyanka Choudhary wrote:
> Hello,
> I'm using MAT-LAB programming for my project work, and for the same I 
> have to read columns of the various sequences simultaneously in a single 
> array. For that I used the 'fastaread' function and I got my sequences 
> in a matrix as cell array like this
> 
> Seq=[4x1 char]
> 
> And when I'm reading this using 'for' loop I'm getting values like this,
> 
> S=[1x439 char]
>    [1x404 char]
>    [1x404 char]
>    [1x343 char]
> 
> But instead I want the columns of the sequences used, as like
> s1=M
>     P
>     I
>     L
> 
> As, I want to use only the columns for my further calculations.
> So, Please if anyone can look into my problem.


What should happen past 343 when data does not exist for all 4 rows ?

The following uses blanks where the row data does not exist:

newS = char(S);

after which newS(:,K) would be the K'th column
0
Reply Walter 7/26/2010 9:10:54 PM

us - 

again an elegant solution
r=cellfun(@(x) x(ix),s)
and again, I am trying to generalize it a bit further and am stuck.

I have a one dim cell array with matrices of different sizes. I.e.

ms = {magic(4),magic(2),magic(5)};

From each of those matrices I want to select only certain elements, given by col and row:

col = {3,1,4};
row = {[0,1,1,1],[1,0],[0,1,0,0,1]};

Now a generalized form of us's statement should yield

r=cellfun(@(x) x(row,col),ms) and uni = 0 of course

r{1} = {[10,6,15]};
r{2} = {1};
r{3} = {[14,2]};

Any thoughts are appreciated, thanks
0
Reply Emil 7/27/2010 8:55:07 AM

"Emil " <emil4ml@gmail.com> wrote in message <i2m6tb$6sa$1@fred.mathworks.com>...
> us - 
> 
> again an elegant solution
> r=cellfun(@(x) x(ix),s)
> and again, I am trying to generalize it a bit further and am stuck.
> 
> I have a one dim cell array with matrices of different sizes. I.e.
> 
> ms = {magic(4),magic(2),magic(5)};
> 
> From each of those matrices I want to select only certain elements, given by col and row:
> 
> col = {3,1,4};
> row = {[0,1,1,1],[1,0],[0,1,0,0,1]};
> 
> Now a generalized form of us's statement should yield
> 
> r=cellfun(@(x) x(row,col),ms) and uni = 0 of course
> 
> r{1} = {[10,6,15]};
> r{2} = {1};
> r{3} = {[14,2]};
> 
> Any thoughts are appreciated, thanks

one of the many solutions
- in this case, ARRAYFUN is more appropriate...

     m={magic(4),magic(2),magic(5)};
     cix={3,1,4};
     rix={[0,1,1,1],[1,0],[0,1,0,0,1]};
     r=arrayfun(@(x) m{x}(find(rix{x}),cix{x}),1:numel(m),'uni',false);
     r{1}.'
%    ans = 10 6 15

us
0
Reply us 7/27/2010 9:38:03 AM

A beautiful solution, thanks
0
Reply Emil 7/27/2010 10:51:04 AM

Walter Roberson <roberson@hushmail.com> wrote in message <i2ktsd$ij6$2@canopus.cc.umanitoba.ca>...
> Priyanka Choudhary wrote:
> > Hello,
> > I'm using MAT-LAB programming for my project work, and for the same I 
> > have to read columns of the various sequences simultaneously in a single 
> > array. For that I used the 'fastaread' function and I got my sequences 
> > in a matrix as cell array like this
> > 
> > Seq=[4x1 char]
> > 
> > And when I'm reading this using 'for' loop I'm getting values like this,
> > 
> > S=[1x439 char]
> >    [1x404 char]
> >    [1x404 char]
> >    [1x343 char]
> > 
> > But instead I want the columns of the sequences used, as like
> > s1=M
> >     P
> >     I
> >     L
> > 
> > As, I want to use only the columns for my further calculations.
> > So, Please if anyone can look into my problem.
> 
> 
> What should happen past 343 when data does not exist for all 4 rows ?
> 
> The following uses blanks where the row data does not exist:
> 
> newS = char(S);
> 
> after which newS(:,K) would be the K'th column

Thanks a lot... I did'nt new I was doing such a silly mistake. It worked very easily.

After that I had a new problem with another syntax used with 'for' loop, which is
            
for i=1:1:num
    if i==1
        A_initial=[ones(1,num_act) zeros(1,num_act*(num-i))]
    
    elseif i>1 & i~=num
            A_initial=[A_initial;zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))]
        
        elseif i==num
                A_initial=[zeros(1,num_act*(i-1)) ones(1,num_act)]
    end
end

In this loop everything is running fine except the first 'elseif' statement.
Need a help...!
Thanks...
0
Reply Priyanka 7/27/2010 4:36:04 PM

Dear Priyanka,

>     elseif i>1 & i~=num

> In this loop everything is running fine except the first 'elseif' statement.
> Need a help...!

Then do us the favor and specify the problems with this line. Any error messages? Unexpected results? An MLint warning about using &&?

Kind regards, Jan
0
Reply Jan 7/27/2010 10:01:04 PM

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i2nkv0$94g$1@fred.mathworks.com>...
> Dear Priyanka,
> 
> >     elseif i>1 & i~=num
> 
> > In this loop everything is running fine except the first 'elseif' statement.
> > Need a help...!
> 
> Then do us the favor and specify the problems with this line. Any error messages? Unexpected results? An MLint warning about using &&?
> 
> Kind regards, Jan

Hello Jan

Thanks for looking into my problem.

For that statement first the error generated was "not a proper CAT argument"and "dimensions mismatch" but now the loop is not ending, going for infinite iterations and one more thing is that in the statement

A_initial=[A_initial;zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))]

First "A_initial" is shown as an error by having a zigzag underline.

And when I'm running the same loop for a smaller value then the A_initial matrix for that 'elseif' statement is giving results of only the last iteration, not the whole loop.

Please see, if U can help...!

Thanks a lot again...!
0
Reply Priyanka 7/28/2010 5:13:04 AM

Dear Priyanka,

> For that statement first the error generated was "not a proper CAT argument"and "dimensions mismatch" but now the loop is not ending, going for infinite iterations and one more thing is that in the statement 
> A_initial=[A_initial;zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))]

The loop is going through infinite, although it is control by "for i = 1:num" ?! No, i is not going to infinite. It stops regularly at num. It is just really slow, because you let the variable A_initial grow in each loop. This *must* be slow. Please search the docs and this newsgroup for the term "preallocation".

> First "A_initial" is shown as an error by having a zigzag underline.

Even Matlab tries to tell you, that this is slow: please move the mouse to the underlined term and read the tooltip.
 
> And when I'm running the same loop for a smaller value then the A_initial matrix for that 'elseif' statement is giving results of only the last iteration, not the whole loop.

Not surprising, because you overwrite A_initial in the last iteration.
So at first take a look on how to solve this with y loop:
  A_initial = zeros(num, num_act * num);  % Allocate at once!!!
  for i = 1:num
     a = num_act * (i -1) + 1;
     b = a + num_act - 1;
     A_initial(i, a:b) = 1;
  end
There is no need check for the first or last iteration at all. Even your original code runs find without it. "zeros(1,num_act*(i-1))" replies an empty matrix for i==1, so there is no need to catch this explicitely.

Jan
0
Reply Jan 7/28/2010 8:08:04 PM

Hello Jan

> > For that statement first the error generated was "not a proper CAT argument"and "dimensions mismatch" but now the loop is not ending, going for infinite iterations and one more thing is that in the statement 
> > A_initial=[A_initial;zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))]
> 
> The loop is going through infinite, although it is control by "for i = 1:num" ?! No, i is not going to infinite. It stops regularly at num. It is just really slow, because you let the variable A_initial grow in each loop. This *must* be slow. Please search the docs and this newsgroup for the term "preallocation".
> 
> > First "A_initial" is shown as an error by having a zigzag underline.
> 
> Even Matlab tries to tell you, that this is slow: please move the mouse to the underlined term and read the tooltip.
>  
> > And when I'm running the same loop for a smaller value then the A_initial matrix for that 'elseif' statement is giving results of only the last iteration, not the whole loop.
> 
> Not surprising, because you overwrite A_initial in the last iteration.
> So at first take a look on how to solve this with y loop:
>   A_initial = zeros(num, num_act * num);  % Allocate at once!!!
>   for i = 1:num
>      a = num_act * (i -1) + 1;
>      b = a + num_act - 1;
>      A_initial(i, a:b) = 1;
>   end
> There is no need check for the first or last iteration at all. Even your original code runs find without it. "zeros(1,num_act*(i-1))" replies an empty matrix for i==1, so there is no need to catch this explicitely.
>

Thanks a lot...
I got my problem solved by using the 'cat' command

A_initial2=[]
for i=1:!:num
     if i==1
            A_initial1=[ones(1,num_act) zeros(1,num_act*(num-i))]
     elseif i>1 & i~=num
            Z=[zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))]
            A_initial2=cat(1,A_initial,Z)
     elseif i==num
            A_initial3=[zeros(1,num_act*(i-1)) ones(1,num_act)]
    end
end

and then i finaly attached all these by using

  A_final=[A_initial1;A_initial2;A_initial3]

Once again, Thanks a lot for looking into my problem.

Priyanka
0
Reply Priyanka 7/29/2010 5:23:03 AM

10 Replies
134 Views

(page loaded in 0.108 seconds)


Reply: