<?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: Working with Arrays of Structures</title>
	<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/</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>Sun, 08 Nov 2009 05:16:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15634</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 30 Nov 2006 20:54:25 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15634</guid>
		<description>Oliver-

Here's the equivalent for-loop if &lt;kbd&gt;f&lt;/kbd&gt; is a vector:

&lt;pre&gt;
for i=1:numel(f)
   [ml{i} nl{i}] = find(f(i).h == 4)
end

isequal(m,ml)
isequal(n,nl)

&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Oliver-</p>
<p>Here&#8217;s the equivalent for-loop if <kbd>f</kbd> is a vector:</p>
<pre>
for i=1:numel(f)
   [ml{i} nl{i}] = find(f(i).h == 4)
end

isequal(m,ml)
isequal(n,nl)
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15554</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Thu, 30 Nov 2006 03:32:48 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15554</guid>
		<description>Loren,

This column introduced me to the "arrayfun," et al. and helped me see a potential use for function handles.  After reviewing the documentation, I can follow part of what's going on.

At the end of your first example, we have two cell array variables, m &#38; n.  The entire purpose of this batch of code is to generate the indices that will allow subsequent code to find the values indicated.  Based on your example structure arrays, I expect the output for the first example to be something like:

1, 4

And, for the second:

1, 3
2, 3
2, 6

But, when I run your examples, I don't know where to find these indices.  I mean, I've poked around in these output cell arrays and I can find the values I know should be someplace.  But I don't understand why they ended up where they are and why some of the cells are empty.

Maybe you can add a bit more detail about what is happening.  Maybe you can write a batch of code using for loops that produces the same results so we can see the data flow.

However, even if I did understand all the details, I'm not sure if I'd ever use this technique.  All of us here who looked at this don't think our coworkers would follow what is going on.</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>This column introduced me to the &#8220;arrayfun,&#8221; et al. and helped me see a potential use for function handles.  After reviewing the documentation, I can follow part of what&#8217;s going on.</p>
<p>At the end of your first example, we have two cell array variables, m &amp; n.  The entire purpose of this batch of code is to generate the indices that will allow subsequent code to find the values indicated.  Based on your example structure arrays, I expect the output for the first example to be something like:</p>
<p>1, 4</p>
<p>And, for the second:</p>
<p>1, 3<br />
2, 3<br />
2, 6</p>
<p>But, when I run your examples, I don&#8217;t know where to find these indices.  I mean, I&#8217;ve poked around in these output cell arrays and I can find the values I know should be someplace.  But I don&#8217;t understand why they ended up where they are and why some of the cells are empty.</p>
<p>Maybe you can add a bit more detail about what is happening.  Maybe you can write a batch of code using for loops that produces the same results so we can see the data flow.</p>
<p>However, even if I did understand all the details, I&#8217;m not sure if I&#8217;d ever use this technique.  All of us here who looked at this don&#8217;t think our coworkers would follow what is going on.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15462</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 28 Nov 2006 21:57:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15462</guid>
		<description>Dan-

I am not sure that I completely understand your problem.  But here's an idea to start you off.  Try using ndgrid to get indices for your various fields.

&lt;pre&gt;
S.filtSize=[10,20,30];
S.name={’john’,'george’};
S.type={@hann};
[filtInd, nameInd, typeInd] = ndgrid(length(S.filtSize),length(S.name),length(S.type);

filtInd =
     1     1
     2     2
     3     3
nameInd =
     1     2
     1     2
     1     2
typeInd =
     1     1
     1     1
     1     1

&lt;/pre&gt;

Using triplets of those indices gets you all the combinations of inputs.

--Loren</description>
		<content:encoded><![CDATA[<p>Dan-</p>
<p>I am not sure that I completely understand your problem.  But here&#8217;s an idea to start you off.  Try using ndgrid to get indices for your various fields.</p>
<pre>
S.filtSize=[10,20,30];
S.name={’john’,'george’};
S.type={@hann};
[filtInd, nameInd, typeInd] = ndgrid(length(S.filtSize),length(S.name),length(S.type);

filtInd =
     1     1
     2     2
     3     3
nameInd =
     1     2
     1     2
     1     2
typeInd =
     1     1
     1     1
     1     1
</pre>
<p>Using triplets of those indices gets you all the combinations of inputs.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15460</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 28 Nov 2006 21:40:27 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15460</guid>
		<description>Reza-

That works fine if all the original arrays are the same length.  But that's not true for the extended example with g.

Then I get this error message:

&lt;pre&gt;
??? Error using ==&gt; cat
CAT arguments dimensions are not consistent.

&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Reza-</p>
<p>That works fine if all the original arrays are the same length.  But that&#8217;s not true for the extended example with g.</p>
<p>Then I get this error message:</p>
<pre>
??? Error using ==> cat
CAT arguments dimensions are not consistent.
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: reza</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15459</link>
		<dc:creator>reza</dc:creator>
		<pubDate>Tue, 28 Nov 2006 21:25:53 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15459</guid>
		<description>how about:

[m,n] = find(cat(1,f.h) == 4)

cat(1,f.h) creates an array by concatinating the two lists produced by f.h on the first axis (vertically).</description>
		<content:encoded><![CDATA[<p>how about:</p>
<p>[m,n] = find(cat(1,f.h) == 4)</p>
<p>cat(1,f.h) creates an array by concatinating the two lists produced by f.h on the first axis (vertically).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan K</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15434</link>
		<dc:creator>Dan K</dc:creator>
		<pubDate>Mon, 27 Nov 2006 21:58:38 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15434</guid>
		<description>Loren,
 Actually the area where I tend to run into the most problems is in constructing my structure array.  I have a situation where I get back a collection of possible options (e.g.
S.filtSize=[10,20,30];S.name={'john','george'};
S.type={@hann};

I spent quite a while trying to find a non loop method for creating a structure array with all possible combinations of settings (taking one from each category) 
example: S(1)=struct('filtSize',10,'name',john','type',@hann);
S(2)=struct('filtSize',20,'name',john','type',@hann);...

This is a simple example but in the where creating the struct array from a cell array would work, but in actuality I have some 10 fields with any number of entries of various types in each field.  I've been looking at this post trying to find a way to use the techniques you describe to address this without any luck... Any thoughts?</description>
		<content:encoded><![CDATA[<p>Loren,<br />
 Actually the area where I tend to run into the most problems is in constructing my structure array.  I have a situation where I get back a collection of possible options (e.g.<br />
S.filtSize=[10,20,30];S.name={&#8217;john&#8217;,'george&#8217;};<br />
S.type={@hann};</p>
<p>I spent quite a while trying to find a non loop method for creating a structure array with all possible combinations of settings (taking one from each category)<br />
example: S(1)=struct(&#8217;filtSize&#8217;,10,&#8217;name&#8217;,john&#8217;,'type&#8217;,@hann);<br />
S(2)=struct(&#8217;filtSize&#8217;,20,&#8217;name&#8217;,john&#8217;,'type&#8217;,@hann);&#8230;</p>
<p>This is a simple example but in the where creating the struct array from a cell array would work, but in actuality I have some 10 fields with any number of entries of various types in each field.  I&#8217;ve been looking at this post trying to find a way to use the techniques you describe to address this without any luck&#8230; Any thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15399</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 27 Nov 2006 13:14:16 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15399</guid>
		<description>Jessee-

arrayfun can be much faster than looping.  First, results are automatically preallocated in size.  Second, the loop inside means that MATLAB doesn't reinterpret the statement each time and pieces of the code can't have side effects that change results for the rest of the array.  arrayfun can take advantage of this in ways that it would be hard or impossible to do in a for loop.

--Loren</description>
		<content:encoded><![CDATA[<p>Jessee-</p>
<p>arrayfun can be much faster than looping.  First, results are automatically preallocated in size.  Second, the loop inside means that MATLAB doesn&#8217;t reinterpret the statement each time and pieces of the code can&#8217;t have side effects that change results for the rest of the array.  arrayfun can take advantage of this in ways that it would be hard or impossible to do in a for loop.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jessee</title>
		<link>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15398</link>
		<dc:creator>Jessee</dc:creator>
		<pubDate>Mon, 27 Nov 2006 12:58:57 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/11/24/working-with-arrays-of-structures/#comment-15398</guid>
		<description>In general, is arrayfun faster than looping?  It seems to me that arrayfun would have to perform a loop anyway.</description>
		<content:encoded><![CDATA[<p>In general, is arrayfun faster than looping?  It seems to me that arrayfun would have to perform a loop anyway.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
