<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Iterating over Non-Numeric Values</title>
	<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Mon, 23 Nov 2009 00:48:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-29603</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 18 Jul 2008 14:53:31 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-29603</guid>
		<description>Ljubomir-

That's the same way I iterated above.  It's a bit clunky getting the contents of the scalar cell, but it is the way to iterate on a cell array.

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>That&#8217;s the same way I iterated above.  It&#8217;s a bit clunky getting the contents of the scalar cell, but it is the way to iterate on a cell array.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-29602</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Fri, 18 Jul 2008 14:43:27 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-29602</guid>
		<description>Got caught by this today - iterate through strings list:

&#62;&#62; for a = {'b', 'c'}; a, end;
a = 'b'
a = 'c'

But actually

&#62;&#62; whos a
  Name      Size            Bytes  Class    Attributes
  a         1x1                62  cell 

is not a string - one needs to

&#62;&#62; for a = {'b', 'c'}; a=a{1}, end;

which I find inpractical + ugly. Am I missing something/is there a better way to iterate through strings list?

thanks,
Ljubomir</description>
		<content:encoded><![CDATA[<p>Got caught by this today - iterate through strings list:</p>
<p>&gt;&gt; for a = {&#8217;b', &#8216;c&#8217;}; a, end;<br />
a = &#8216;b&#8217;<br />
a = &#8216;c&#8217;</p>
<p>But actually</p>
<p>&gt;&gt; whos a<br />
  Name      Size            Bytes  Class    Attributes<br />
  a         1&#215;1                62  cell </p>
<p>is not a string - one needs to</p>
<p>&gt;&gt; for a = {&#8217;b', &#8216;c&#8217;}; a=a{1}, end;</p>
<p>which I find inpractical + ugly. Am I missing something/is there a better way to iterate through strings list?</p>
<p>thanks,<br />
Ljubomir</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: per isakson</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27881</link>
		<dc:creator>per isakson</dc:creator>
		<pubDate>Fri, 28 Mar 2008 16:01:15 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27881</guid>
		<description>Ljubomir-

The foreach-functionality is included in Matlab's FOR - isn't it?. More often than not, my for-loops are through cell and structured arrays. The arrays may hold anything even funtionhandles of nested functions. Are cell arrays less powerful than collections?

It took me some time to appreiciate the full power of the for-loops in Matlab. 

- per</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>The foreach-functionality is included in Matlab&#8217;s FOR - isn&#8217;t it?. More often than not, my for-loops are through cell and structured arrays. The arrays may hold anything even funtionhandles of nested functions. Are cell arrays less powerful than collections?</p>
<p>It took me some time to appreiciate the full power of the for-loops in Matlab. </p>
<p>- per</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27879</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 28 Mar 2008 15:02:05 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27879</guid>
		<description>Ljubomir-

Depending on the collection, there's cellfun, arrayfun, and structfun.  And yes, you can use A(:).' to make a single row.  Or you can use reshape(A,1,[]).

I'm glad the editor warning on loop variables is helpful to you.

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>Depending on the collection, there&#8217;s cellfun, arrayfun, and structfun.  And yes, you can use A(:).&#8217; to make a single row.  Or you can use reshape(A,1,[]).</p>
<p>I&#8217;m glad the editor warning on loop variables is helpful to you.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27877</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Fri, 28 Mar 2008 14:50:09 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-27877</guid>
		<description>More generally, Matlab lacks "foreach" construct where one can iterate through a collection without the need to introduce an index.** 

I have settled for a variant of solution 2. I'd use " for str = fn(:)' ". The column operator is to make sure I get to a row regardless if " fn " is row or column. (ie context independent - maybe there is a single "make row" operator instead of " (:)' " ?)

- Ljubomir


**Unneeded indices are bad. Not needing them for anything else but to get to the value, one tends to choose the obvious i,j,k without sufficient attention so may well overwrite the same named loop vars in an outer loop. The editor warns so is of great help here.</description>
		<content:encoded><![CDATA[<p>More generally, Matlab lacks &#8220;foreach&#8221; construct where one can iterate through a collection without the need to introduce an index.** </p>
<p>I have settled for a variant of solution 2. I&#8217;d use &#8221; for str = fn(:)&#8217; &#8220;. The column operator is to make sure I get to a row regardless if &#8221; fn &#8221; is row or column. (ie context independent - maybe there is a single &#8220;make row&#8221; operator instead of &#8221; (:)&#8217; &#8221; ?)</p>
<p>- Ljubomir</p>
<p>**Unneeded indices are bad. Not needing them for anything else but to get to the value, one tends to choose the obvious i,j,k without sufficient attention so may well overwrite the same named loop vars in an outer loop. The editor warns so is of great help here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25811</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 05 Feb 2008 13:18:13 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25811</guid>
		<description>Andrew-

I don't see how you can do it simply for your set up since you have both cells and numeric arrays and the same functions don't operate on both and structfun wants one function (though I guess it could be a complicated one).  Besides, for non-uniform output, structfun outputs a struct so I can't see how you're better off.

--Loren</description>
		<content:encoded><![CDATA[<p>Andrew-</p>
<p>I don&#8217;t see how you can do it simply for your set up since you have both cells and numeric arrays and the same functions don&#8217;t operate on both and structfun wants one function (though I guess it could be a complicated one).  Besides, for non-uniform output, structfun outputs a struct so I can&#8217;t see how you&#8217;re better off.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25810</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Tue, 05 Feb 2008 13:00:50 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25810</guid>
		<description>Thanks Loren for the reply, but I am looking for a solution that takes the structure and places its contents into a cell array, without having to specify the individual fieldnames. That is why I was looking at structfun. In the end I think struct2cell will do the trick with the use of num2cell, but for the "fun" of it, could it be done with structfun?</description>
		<content:encoded><![CDATA[<p>Thanks Loren for the reply, but I am looking for a solution that takes the structure and places its contents into a cell array, without having to specify the individual fieldnames. That is why I was looking at structfun. In the end I think struct2cell will do the trick with the use of num2cell, but for the &#8220;fun&#8221; of it, could it be done with structfun?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25767</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 04 Feb 2008 15:09:55 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25767</guid>
		<description>Andrew-

I think num2cell is what you need to spread your numeric array out into a cell.  Try this:

&lt;pre class='code'&gt;
s.a={'first', 'second'};
s.b=[1.1 1.2];
s.c=[30.1 33.2];

mycell= [s.a ; num2cell(s.b); num2cell(s.c)]'

mycell = 
    'first'     [1.1000]    [30.1000]
    'second'    [1.2000]    [33.2000]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Andrew-</p>
<p>I think num2cell is what you need to spread your numeric array out into a cell.  Try this:</p>
<pre class='code'>
s.a={'first', 'second'};
s.b=[1.1 1.2];
s.c=[30.1 33.2];

mycell= [s.a ; num2cell(s.b); num2cell(s.c)]'

mycell =
    'first'     [1.1000]    [30.1000]
    'second'    [1.2000]    [33.2000]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25711</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Sun, 03 Feb 2008 13:12:41 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25711</guid>
		<description>I am interested in the structfun and anonymous function solution, but my struct is a struct of arrays of mixed type as follows:
s.a={'first', 'second'};
s.b=[1.1 1.2];
s.c=[30.1 33.2];
I am looking to gather the output into a single cell array like so:
{'first', 1.1, 30.1; 'second', 1.2, 33.2}
I have tried structfun and arrayfun also without success so far.</description>
		<content:encoded><![CDATA[<p>I am interested in the structfun and anonymous function solution, but my struct is a struct of arrays of mixed type as follows:<br />
s.a={&#8217;first&#8217;, &#8217;second&#8217;};<br />
s.b=[1.1 1.2];<br />
s.c=[30.1 33.2];<br />
I am looking to gather the output into a single cell array like so:<br />
{&#8217;first&#8217;, 1.1, 30.1; &#8217;second&#8217;, 1.2, 33.2}<br />
I have tried structfun and arrayfun also without success so far.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25590</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 31 Jan 2008 13:29:33 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2007/08/15/iterating-over-non-numeric-values/#comment-25590</guid>
		<description>Hussein-

Your questions do not have simple answers and are simply too general to give you good answers.  They depend very much on circumstances.  Vectorization sometimes helps a lot, and sometimes not.  The amount of savings depends on all the details.  You can call a function and return a vector of structures.  See the documentation.  Input vectors don't have to be the same size for all functions, but it depends what function(s) you are referring to.

--Loren</description>
		<content:encoded><![CDATA[<p>Hussein-</p>
<p>Your questions do not have simple answers and are simply too general to give you good answers.  They depend very much on circumstances.  Vectorization sometimes helps a lot, and sometimes not.  The amount of savings depends on all the details.  You can call a function and return a vector of structures.  See the documentation.  Input vectors don&#8217;t have to be the same size for all functions, but it depends what function(s) you are referring to.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
</channel>
</rss>
