<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485</link>
    <title>MATLAB Central Newsreader - Vectorization with cell array</title>
    <description>Feed for thread: Vectorization with cell array</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 23 Jan 2008 09:18:01 -0500</pubDate>
      <title>Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#410798</link>
      <author>Andrew </author>
      <description>I have a problem whereby I wish to vectorize the assignment&lt;br&gt;
to a cell array. If I have&lt;br&gt;
ndx=1:2;&lt;br&gt;
B(ndx)={'First', 'Second'};&lt;br&gt;
C(ndx)=[1 2];&lt;br&gt;
I can then create a structure with a cell array by&lt;br&gt;
Test.test(ndx(1),:)={B(ndx(1)), C(ndx(1))};&lt;br&gt;
and do the same for ndx(2), but how to do the following:&lt;br&gt;
Test.test(ndx,:)={B(ndx), C(ndx)};&lt;br&gt;
hence getting rid of the for loop. I have been looking at&lt;br&gt;
functions like cellfun and arrayfun, but no luck yet.&lt;br&gt;
&lt;br&gt;
Thanks in advance for the help.&lt;br&gt;
Andrew</description>
    </item>
    <item>
      <pubDate>Wed, 23 Jan 2008 10:04:02 -0500</pubDate>
      <title>Re: Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#410809</link>
      <author>us</author>
      <description>&quot;Andrew &quot;:&lt;br&gt;
&amp;lt;SNIP cell acrobatics...&lt;br&gt;
&lt;br&gt;
one of the solutions&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b={'first','second'};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c=[1,2];&lt;br&gt;
% the engine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear s v; % save old stuff!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;v=cellfun(@(a,b) {a,b},b,num2cell(c),'uni',false);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s=struct('f',v);&lt;br&gt;
% the result&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.f&lt;br&gt;
% ans = 'first' [1]&lt;br&gt;
% ans = 'second' [2]&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Wed, 23 Jan 2008 10:57:52 -0500</pubDate>
      <title>Re: Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#410827</link>
      <author>Yumnam Kirani Singh</author>
      <description>Have you tried this way.&lt;br&gt;
Test.Test(ndx)={B;C}</description>
    </item>
    <item>
      <pubDate>Wed, 23 Jan 2008 15:55:03 -0500</pubDate>
      <title>Re: Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#410906</link>
      <author>Andrew </author>
      <description>&quot;us &quot; &amp;lt;us@neurol.unizh.ch&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fn73ei$afj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Andrew &quot;:&lt;br&gt;
&amp;gt; &amp;lt;SNIP cell acrobatics...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; one of the solutions&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % the data&lt;br&gt;
&amp;gt;      b={'first','second'};&lt;br&gt;
&amp;gt;      c=[1,2];&lt;br&gt;
&amp;gt; % the engine&lt;br&gt;
&amp;gt;      clear s v; % save old stuff!&lt;br&gt;
&amp;gt;      v=cellfun(@(a,b) {a,b},b,num2cell(c),'uni',false);&lt;br&gt;
&amp;gt;      s=struct('f',v);&lt;br&gt;
&amp;gt; % the result&lt;br&gt;
&amp;gt;      s.f&lt;br&gt;
&amp;gt; % ans = 'first' [1]&lt;br&gt;
&amp;gt; % ans = 'second' [2]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; us&lt;br&gt;
Thanks but these solutions do not quite achieve what I am&lt;br&gt;
looking for. In the example above v is a cell array of cell&lt;br&gt;
arrays. I am looking to produce a single cell array where&lt;br&gt;
v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on. I&lt;br&gt;
guess I could do [v{:}] and then reshape the output, but a&lt;br&gt;
neater solution would be preferable.&lt;br&gt;
&lt;br&gt;
Andrew</description>
    </item>
    <item>
      <pubDate>Wed, 23 Jan 2008 17:29:02 -0500</pubDate>
      <title>Re: Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#410927</link>
      <author>us</author>
      <description>&quot;Andrew &quot;:&lt;br&gt;
&amp;lt;SNIP changing the problem...&lt;br&gt;
&lt;br&gt;
&amp;gt; Thanks but these solutions do not quite achieve what I am&lt;br&gt;
&amp;gt; looking for.&lt;br&gt;
&amp;gt; In the example above v is a cell array of cell&lt;br&gt;
&amp;gt; arrays. I am looking to produce a single cell array where&lt;br&gt;
&amp;gt; v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on...&lt;br&gt;
&lt;br&gt;
well, what you want NOW is NOT what you wanted before...&lt;br&gt;
running your initial example yields this&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Test.test(1,1) % &amp;lt;- array index&lt;br&gt;
%          {1x1 cell}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Test.test{1,1} % &amp;lt;- cell contents&lt;br&gt;
%          'First' % -&amp;gt; now your v(1,1)&lt;br&gt;
&lt;br&gt;
your &amp;lt;acutal&amp;gt; example could be achieved by this&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear s;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b={'first','second'};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c=[1,2];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d=num2cell(c);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.f=[b.',d.'];&lt;br&gt;
% some results&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.f&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.f(1,1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.f(2,2)&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Thu, 24 Jan 2008 09:36:02 -0500</pubDate>
      <title>Re: Vectorization with cell array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/162485#411037</link>
      <author>Andrew </author>
      <description>&quot;us &quot; &amp;lt;us@neurol.unizh.ch&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fn7tgu$g8r$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Andrew &quot;:&lt;br&gt;
&amp;gt; &amp;lt;SNIP changing the problem...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks but these solutions do not quite achieve what I am&lt;br&gt;
&amp;gt; &amp;gt; looking for.&lt;br&gt;
&amp;gt; &amp;gt; In the example above v is a cell array of cell&lt;br&gt;
&amp;gt; &amp;gt; arrays. I am looking to produce a single cell array where&lt;br&gt;
&amp;gt; &amp;gt; v(1,1) is 'first', v(1,2)=1, v(2,1)='second' and so on...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; well, what you want NOW is NOT what you wanted before...&lt;br&gt;
&amp;gt; running your initial example yields this&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      Test.test(1,1) % &amp;lt;- array index&lt;br&gt;
&amp;gt; %          {1x1 cell}&lt;br&gt;
&amp;gt;      Test.test{1,1} % &amp;lt;- cell contents&lt;br&gt;
&amp;gt; %          'First' % -&amp;gt; now your v(1,1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; your &amp;lt;acutal&amp;gt; example could be achieved by this&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      clear s;&lt;br&gt;
&amp;gt;      b={'first','second'};&lt;br&gt;
&amp;gt;      c=[1,2];&lt;br&gt;
&amp;gt;      d=num2cell(c);&lt;br&gt;
&amp;gt;      s.f=[b.',d.'];&lt;br&gt;
&amp;gt; % some results&lt;br&gt;
&amp;gt;      s.f&lt;br&gt;
&amp;gt;      s.f(1,1)&lt;br&gt;
&amp;gt;      s.f(2,2)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; us&lt;br&gt;
Thanks for the reply again. This is what I wanted to do and&lt;br&gt;
if you do run the example I gave in my first post using&lt;br&gt;
ndx(1) and ndx(2) you will end up with the same cell array&lt;br&gt;
as you acheived by vectorization.&lt;br&gt;
&lt;br&gt;
I appreciate the help! Andrew</description>
    </item>
  </channel>
</rss>

