<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Acting on Specific Elements in a Matrix</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: max</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32428</link>
		<dc:creator>max</dc:creator>
		<pubDate>Fri, 19 Aug 2011 13:11:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32428</guid>
		<description>thanks!
wasnt aware of this before, solved a bug i had been working on for two days already.</description>
		<content:encoded><![CDATA[<p>thanks!<br />
wasnt aware of this before, solved a bug i had been working on for two days already.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32262</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 18 May 2011 17:50:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32262</guid>
		<description>Jan-

As far as I know, there is not a simple 1-liner to go from subscripts to logicals without using sub2ind.  Sorry you don&#039;t care for that solution.

--Loren</description>
		<content:encoded><![CDATA[<p>Jan-</p>
<p>As far as I know, there is not a simple 1-liner to go from subscripts to logicals without using sub2ind.  Sorry you don&#8217;t care for that solution.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32261</link>
		<dc:creator>Jan</dc:creator>
		<pubDate>Wed, 18 May 2011 17:37:05 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-32261</guid>
		<description>Hi Loren,

i have red this post, but didn&#039;t find what I was looking for. Is there a easy way to make a logical matrix from subscript indices like the function find does but the other way round. Under method number 2 in your post you gave a small example what happens when you use subscripts to address the selected elements of a matrix. You get the full matrix back and not only the selected elements. I think the easiest way would be a logical to adress and get back only the selected elements. Using the function sub2ind is too much bother in my opinion.

-Jan</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>i have red this post, but didn&#8217;t find what I was looking for. Is there a easy way to make a logical matrix from subscript indices like the function find does but the other way round. Under method number 2 in your post you gave a small example what happens when you use subscripts to address the selected elements of a matrix. You get the full matrix back and not only the selected elements. I think the easiest way would be a logical to adress and get back only the selected elements. Using the function sub2ind is too much bother in my opinion.</p>
<p>-Jan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31954</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 10 Jan 2011 16:19:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31954</guid>
		<description>Kharabela-

Check out the functions find and ismember.

--Loren</description>
		<content:encoded><![CDATA[<p>Kharabela-</p>
<p>Check out the functions find and ismember.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kharabela</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31950</link>
		<dc:creator>Kharabela</dc:creator>
		<pubDate>Sat, 08 Jan 2011 15:07:14 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31950</guid>
		<description>hi every body
pls help


How to find the position of a matrix element...
thanking you...</description>
		<content:encoded><![CDATA[<p>hi every body<br />
pls help</p>
<p>How to find the position of a matrix element&#8230;<br />
thanking you&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joshua</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31295</link>
		<dc:creator>joshua</dc:creator>
		<pubDate>Fri, 30 Apr 2010 03:10:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31295</guid>
		<description>interesting.  ok, i guess i&#039;ll have to live with that ;)
thanks for your help...</description>
		<content:encoded><![CDATA[<p>interesting.  ok, i guess i&#8217;ll have to live with that ;)<br />
thanks for your help&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31294</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 29 Apr 2010 12:46:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31294</guid>
		<description>Joshua-

The first line takes no time since the 2 arrays are identical and MATLAB only does lazy copy or copy on write (see blogs on memory to learn more about this).  Once an element is changing, MATLAB makes a full copy of the original array to be sure it doesn&#039;t get modified improperly.

Again, the best way to drop elements is to create the new array and fill it, you&#039;re 3rd way - only the copy needed for the output is created in addition to the original, I believe.  I don&#039;t think you can do better than that.

--Loren</description>
		<content:encoded><![CDATA[<p>Joshua-</p>
<p>The first line takes no time since the 2 arrays are identical and MATLAB only does lazy copy or copy on write (see blogs on memory to learn more about this).  Once an element is changing, MATLAB makes a full copy of the original array to be sure it doesn&#8217;t get modified improperly.</p>
<p>Again, the best way to drop elements is to create the new array and fill it, you&#8217;re 3rd way &#8211; only the copy needed for the output is created in addition to the original, I believe.  I don&#8217;t think you can do better than that.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joshua</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31293</link>
		<dc:creator>joshua</dc:creator>
		<pubDate>Thu, 29 Apr 2010 12:30:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31293</guid>
		<description>hey loren, thanks for the response.  so, the first line of the first two methods, where the copy is made, takes nearly no time.  it is the second line, where i only discard elements of the matrix, that takes time.  that might have been confusing.  in practice, what i do now is something like:

mean(A(:,:,s),3)

where &#039;s&#039; is a vector listing the indices that i care about.

and the reason i care about all this, is because i am doing leave-one-out cross-validation.  i have a bunch of training data, and i am fitting my model, but want to ensure that i have not overfit.  so, i loop through every data point, discard it to fit the model, and then check the model accuracy on it.  the part of the code that takes by far the longest was dropping a matrix from the array.  now, i skip that by doing it all in one line, as shown above.  but now that line just takes about as long as dropping the matrix did, so i didn&#039;t get any  speed up.   

specifically, my code now looks like this:

&lt;pre&gt;
n=100;          % square nxn matrices
n2=n^2;
A0=rand(n,n,n); % generate matrices
for i=1:n       % for each matrix
  
  % method 1 of getting mean
  A1=A0(:,:,2:n); 
  M1=mean(A3,3);   
  
  % method 2 of getting mean
  M2=mean(A0(:,:,2:n),3);  % mean computation for comparison purposes 
end
&lt;/pre&gt;

running the profiler shows that the line to compute M2 takes equally long as the line to compute A1 and M1 combined.  and, if i look carefully, i see that actually computing M2, matlab only spends a fraction of the time computing the mean, the rest is passing the variable to the mean function.

so my question remains: is there anyway for matlab to not take a long time when dropping elements in an array? trying to trick it by putting it all on one line did not seem to work, sadly ;(</description>
		<content:encoded><![CDATA[<p>hey loren, thanks for the response.  so, the first line of the first two methods, where the copy is made, takes nearly no time.  it is the second line, where i only discard elements of the matrix, that takes time.  that might have been confusing.  in practice, what i do now is something like:</p>
<p>mean(A(:,:,s),3)</p>
<p>where &#8216;s&#8217; is a vector listing the indices that i care about.</p>
<p>and the reason i care about all this, is because i am doing leave-one-out cross-validation.  i have a bunch of training data, and i am fitting my model, but want to ensure that i have not overfit.  so, i loop through every data point, discard it to fit the model, and then check the model accuracy on it.  the part of the code that takes by far the longest was dropping a matrix from the array.  now, i skip that by doing it all in one line, as shown above.  but now that line just takes about as long as dropping the matrix did, so i didn&#8217;t get any  speed up.   </p>
<p>specifically, my code now looks like this:</p>
<pre>
n=100;          % square nxn matrices
n2=n^2;
A0=rand(n,n,n); % generate matrices
for i=1:n       % for each matrix

  % method 1 of getting mean
  A1=A0(:,:,2:n);
  M1=mean(A3,3);   

  % method 2 of getting mean
  M2=mean(A0(:,:,2:n),3);  % mean computation for comparison purposes
end
</pre>
<p>running the profiler shows that the line to compute M2 takes equally long as the line to compute A1 and M1 combined.  and, if i look carefully, i see that actually computing M2, matlab only spends a fraction of the time computing the mean, the rest is passing the variable to the mean function.</p>
<p>so my question remains: is there anyway for matlab to not take a long time when dropping elements in an array? trying to trick it by putting it all on one line did not seem to work, sadly ;(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31287</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 26 Apr 2010 13:50:39 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31287</guid>
		<description>Joshua-

mean takes less time since there&#039;s no need to copy the array at all before acting on it.

What exactly is taking so long that you want to go faster?  I doubt you can do better than the 3rd method, which I think makes the fewest temporary intermediate arrays (none).  It only makes a copy of the array to keep and nothing more.

--loren</description>
		<content:encoded><![CDATA[<p>Joshua-</p>
<p>mean takes less time since there&#8217;s no need to copy the array at all before acting on it.</p>
<p>What exactly is taking so long that you want to go faster?  I doubt you can do better than the 3rd method, which I think makes the fewest temporary intermediate arrays (none).  It only makes a copy of the array to keep and nothing more.</p>
<p>&#8211;loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joshua</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31284</link>
		<dc:creator>joshua</dc:creator>
		<pubDate>Fri, 23 Apr 2010 17:58:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-31284</guid>
		<description>i have a question about removing elements from an array based on their indices, not their values.  please consider the following code snippet showing 3 methods for doing such an operation:

&lt;pre&gt;
n=100;          % square nxn matrices
n2=n^2;
A0=rand(n,n,n); % generate matrices
for i=1:n       % for each matrix
  
  % method 1 of dropping a matrix
  A1=A0;         
  A1(:,:,i)=[];  
  
  % method 2 of dropping a matrix
  A2=A0;
  A2(1:n2)=[];    

  % method 3 of dropping a matrix
  A3=A0(:,:,2:n); 
  
  % mean computation for comparison purposes 
  mean(A0,3);   
end
&lt;/pre&gt;

note that upon running profiler, method 3 takes about half the time of method 1, and about 1/3 of the time of method 2.  but the mean computation takes far less time than any!

this quite surprising result (to me) leads me to have the following questions:

1) why does it take so long?
2) is there a way to make it go *much* faster?

many thanks,
joshua</description>
		<content:encoded><![CDATA[<p>i have a question about removing elements from an array based on their indices, not their values.  please consider the following code snippet showing 3 methods for doing such an operation:</p>
<pre>
n=100;          % square nxn matrices
n2=n^2;
A0=rand(n,n,n); % generate matrices
for i=1:n       % for each matrix

  % method 1 of dropping a matrix
  A1=A0;
  A1(:,:,i)=[];  

  % method 2 of dropping a matrix
  A2=A0;
  A2(1:n2)=[];    

  % method 3 of dropping a matrix
  A3=A0(:,:,2:n); 

  % mean computation for comparison purposes
  mean(A0,3);
end
</pre>
<p>note that upon running profiler, method 3 takes about half the time of method 1, and about 1/3 of the time of method 2.  but the mean computation takes far less time than any!</p>
<p>this quite surprising result (to me) leads me to have the following questions:</p>
<p>1) why does it take so long?<br />
2) is there a way to make it go *much* faster?</p>
<p>many thanks,<br />
joshua</p>
]]></content:encoded>
	</item>
</channel>
</rss>

