<?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: Acting on Specific Elements in a Matrix</title>
	<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 &#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:06:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Nickitas</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-30143</link>
		<dc:creator>Nickitas</dc:creator>
		<pubDate>Thu, 26 Mar 2009 02:35:26 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-30143</guid>
		<description>Thank you very much for Method #4.

I've been trying hard to find a way for vectorizing relational matrix manipulation in Matlab.
Maybe there is an even simpler way that I am not aware of, but your #4 works.
I used it with
indices = sub2ind(size(A),rnotA+1,cnotA+1);

Note +1. I basically needed to manipulate the i+1,j+1 neighbors of elements satisfying a condition without relying on a for loop.

Thanks again!

-Nickitas</description>
		<content:encoded><![CDATA[<p>Thank you very much for Method #4.</p>
<p>I&#8217;ve been trying hard to find a way for vectorizing relational matrix manipulation in Matlab.<br />
Maybe there is an even simpler way that I am not aware of, but your #4 works.<br />
I used it with<br />
indices = sub2ind(size(A),rnotA+1,cnotA+1);</p>
<p>Note +1. I basically needed to manipulate the i+1,j+1 neighbors of elements satisfying a condition without relying on a for loop.</p>
<p>Thanks again!</p>
<p>-Nickitas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29441</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Wed, 04 Jun 2008 20:24:12 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29441</guid>
		<description>Hey Loren,

Logical indexing is nice, but not always practical.  We produce hundreds of GB of data every day a subsequently load it into MATLAB.  It’s possible to construct a short array of numerical indices, which contains the subset of the data I may be interested in but keeping a logical index around is not really feasible, either in MATLAB or on disk.

Further, to the comparison of logical vs. numerical indexing (on a smaller footprint example) 

x = rand(2 * 1e9, 1); %16GB of data

tic; l = x &#62; 0.5; toc
Elapsed time is 4.771542 seconds.

tic; i = find(l); toc
Elapsed time is 29.321260 seconds.

&#62;&#62; tic; y=x(l); toc
Elapsed time is 49.782177 seconds.

&#62;&#62; tic; z=x(i); toc
Elapsed time is 51.138457 seconds.

As you can see, there is very little in it for a large set, since most of the computation is likely to be memory bound.  I have to say that I’m disappointed with the implementation of the numeric subscript.  It is not surprising that a logical index must grow the resulting array, since it doesn’t know in advance how many elements may be in the result.  However, to see reallocations on my system monitors during the final subscript call seems unnecessary, since I know *exactly* how many elements there’ll be.  Memory reallocation would actually be the reason why, for large datasets, I’d expect the numerical index to outperform the logical index.

On a similar note, here are two features I’d like to see in MATLAB:
1)	Don’t construct the logical array if you don’t need to:  For example,

i = find(logical expression);

or even

b = logical expression;
i = find(b);
% b no longer used after here

There seems no reason why you have to do construct the temporary logical array first and then follow the computation by another pass over the logical array.  This is slow and wastes an unnecessary amount of memory for a temporary.

2)	Again, suppose I have a large dataset (200+ GB in size).  I want to perform a computation on a subset of it (say 50GB).  I have to make a copy to use the data in the computation, which is a problem, as I’m already riding hard against the amount of available memory.  If I have a list of the indices already, I’d like to use an “indirect” array – an object that holds both indices and the original data and allows me to subscript it as a regular array.  To preempt objections: a) Yes, I could write a class to do it but that’s not very fast. b)  The object could be read only, so that you wouldn’t have to worry about multiple numerical indices with the same value.

Many thanks,

Tom</description>
		<content:encoded><![CDATA[<p>Hey Loren,</p>
<p>Logical indexing is nice, but not always practical.  We produce hundreds of GB of data every day a subsequently load it into MATLAB.  It’s possible to construct a short array of numerical indices, which contains the subset of the data I may be interested in but keeping a logical index around is not really feasible, either in MATLAB or on disk.</p>
<p>Further, to the comparison of logical vs. numerical indexing (on a smaller footprint example) </p>
<p>x = rand(2 * 1e9, 1); %16GB of data</p>
<p>tic; l = x &gt; 0.5; toc<br />
Elapsed time is 4.771542 seconds.</p>
<p>tic; i = find(l); toc<br />
Elapsed time is 29.321260 seconds.</p>
<p>&gt;&gt; tic; y=x(l); toc<br />
Elapsed time is 49.782177 seconds.</p>
<p>&gt;&gt; tic; z=x(i); toc<br />
Elapsed time is 51.138457 seconds.</p>
<p>As you can see, there is very little in it for a large set, since most of the computation is likely to be memory bound.  I have to say that I’m disappointed with the implementation of the numeric subscript.  It is not surprising that a logical index must grow the resulting array, since it doesn’t know in advance how many elements may be in the result.  However, to see reallocations on my system monitors during the final subscript call seems unnecessary, since I know *exactly* how many elements there’ll be.  Memory reallocation would actually be the reason why, for large datasets, I’d expect the numerical index to outperform the logical index.</p>
<p>On a similar note, here are two features I’d like to see in MATLAB:<br />
1)	Don’t construct the logical array if you don’t need to:  For example,</p>
<p>i = find(logical expression);</p>
<p>or even</p>
<p>b = logical expression;<br />
i = find(b);<br />
% b no longer used after here</p>
<p>There seems no reason why you have to do construct the temporary logical array first and then follow the computation by another pass over the logical array.  This is slow and wastes an unnecessary amount of memory for a temporary.</p>
<p>2)	Again, suppose I have a large dataset (200+ GB in size).  I want to perform a computation on a subset of it (say 50GB).  I have to make a copy to use the data in the computation, which is a problem, as I’m already riding hard against the amount of available memory.  If I have a list of the indices already, I’d like to use an “indirect” array – an object that holds both indices and the original data and allows me to subscript it as a regular array.  To preempt objections: a) Yes, I could write a class to do it but that’s not very fast. b)  The object could be read only, so that you wouldn’t have to worry about multiple numerical indices with the same value.</p>
<p>Many thanks,</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29180</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 20 May 2008 16:03:35 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29180</guid>
		<description>Tony-

Here's the documentation for end:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/end.html

The capability has been allowed, I believe, since its introduction as an index (and overloadable with the class system).

--Loren</description>
		<content:encoded><![CDATA[<p>Tony-</p>
<p>Here&#8217;s the documentation for end:</p>
<p><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/end.html" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/techdoc/ref/end.html</a></p>
<p>The capability has been allowed, I believe, since its introduction as an index (and overloadable with the class system).</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Booer</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29179</link>
		<dc:creator>Tony Booer</dc:creator>
		<pubDate>Tue, 20 May 2008 15:59:06 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29179</guid>
		<description>Steve, Loren,

Thanks very much for this solution...

B = A(setdiff(1:end, R), setdiff(1:end, C));

Steve - very neat,
Loren - I had no idea I could use "end" in a function call !

...where's that documented ?</description>
		<content:encoded><![CDATA[<p>Steve, Loren,</p>
<p>Thanks very much for this solution&#8230;</p>
<p>B = A(setdiff(1:end, R), setdiff(1:end, C));</p>
<p>Steve - very neat,<br />
Loren - I had no idea I could use &#8220;end&#8221; in a function call !</p>
<p>&#8230;where&#8217;s that documented ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: helper</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29161</link>
		<dc:creator>helper</dc:creator>
		<pubDate>Mon, 19 May 2008 18:40:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29161</guid>
		<description>I tend to use all methods mentioned based on my need.

What I find interesting is how common a problem it is for (not necessarily beginner) users to run into the issue mentioned in Method #1.  Often users are using subscripting and don't realize they need to convert to linear indices to solve a specific problem.

I find myself having to repeatedly explain the difference and am glad you have addressed the topic here.</description>
		<content:encoded><![CDATA[<p>I tend to use all methods mentioned based on my need.</p>
<p>What I find interesting is how common a problem it is for (not necessarily beginner) users to run into the issue mentioned in Method #1.  Often users are using subscripting and don&#8217;t realize they need to convert to linear indices to solve a specific problem.</p>
<p>I find myself having to repeatedly explain the difference and am glad you have addressed the topic here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luca Balbi</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29149</link>
		<dc:creator>Luca Balbi</dc:creator>
		<pubDate>Mon, 19 May 2008 06:47:11 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29149</guid>
		<description>I definitely prefer, and use as often as possible, logical indexing, possibly keeping the indices stored for future reference only if really needed.
It makes the code much more readable and, as I used to be programming with SQL languages, it exploits the semantic power of the WHERE clauses which are what make DB programming so powerful.</description>
		<content:encoded><![CDATA[<p>I definitely prefer, and use as often as possible, logical indexing, possibly keeping the indices stored for future reference only if really needed.<br />
It makes the code much more readable and, as I used to be programming with SQL languages, it exploits the semantic power of the WHERE clauses which are what make DB programming so powerful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29119</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 16 May 2008 23:02:57 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29119</guid>
		<description>Steve-

Nice reply to Tony's challenge.  You can use end's in the expressions of your first solutions as well:

&lt;pre class="code"&gt;
  A = reshape(1:49, 7, 7);
  R = 1:2:7; % Rows to remove
  C = 2:2:7;
  B = A(setxor(R, 1:end), setxor(C, 1:end));
  % or
  B = A(setdiff(1:end, R), setdiff(1:end, C));
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Steve-</p>
<p>Nice reply to Tony&#8217;s challenge.  You can use end&#8217;s in the expressions of your first solutions as well:</p>
<pre class="code">
  A = reshape(1:49, 7, 7);
  R = 1:2:7; % Rows to remove
  C = 2:2:7;
  B = A(setxor(R, 1:end), setxor(C, 1:end));
  % or
  B = A(setdiff(1:end, R), setdiff(1:end, C));
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29114</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Fri, 16 May 2008 17:10:44 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29114</guid>
		<description>Tony,

With regard to removing rows R and columns C from a matrix, you can use:

  A = reshape(1:49, 7, 7);
  B = A; % for comparison
  R = 1:2:7; % Rows to remove
  C = 2:2:7;
  B(R, :) = [];
  B(:, C) = [];

or:

  A = reshape(1:49, 7, 7);
  R = 1:2:7; % Rows to remove
  C = 2:2:7;
  B = A(setxor(R, 1:7), setxor(C, 1:7));
  % or
  B = A(setdiff(1:7, R), setdiff(1:7, C));

As for the arithmetic/logical combination, that will work ... as long as all your elements of A are finite.

  A = 1:1000;
  B = A.*(A &#62; (17^2)/2);
  all(B == A &#124; B == 0)

  A = 1:1000;
  A([5 982]) = NaN;
  A([7 562]) = Inf;
  A([324 870]) = -Inf;
  B = A.*(A &#62; (17^2)/2);
  all(B == A &#124; B == 0)

Note that not only are elements 5 and 982 of the second B NaN (as you would expect, since any arithmetic operation on a NaN results in a NaN result) but so are elements 324 and 870.  This is because 0*Inf and 0*(-Inf) also both return NaN.</description>
		<content:encoded><![CDATA[<p>Tony,</p>
<p>With regard to removing rows R and columns C from a matrix, you can use:</p>
<p>  A = reshape(1:49, 7, 7);<br />
  B = A; % for comparison<br />
  R = 1:2:7; % Rows to remove<br />
  C = 2:2:7;<br />
  B(R, :) = [];<br />
  B(:, C) = [];</p>
<p>or:</p>
<p>  A = reshape(1:49, 7, 7);<br />
  R = 1:2:7; % Rows to remove<br />
  C = 2:2:7;<br />
  B = A(setxor(R, 1:7), setxor(C, 1:7));<br />
  % or<br />
  B = A(setdiff(1:7, R), setdiff(1:7, C));</p>
<p>As for the arithmetic/logical combination, that will work &#8230; as long as all your elements of A are finite.</p>
<p>  A = 1:1000;<br />
  B = A.*(A &gt; (17^2)/2);<br />
  all(B == A | B == 0)</p>
<p>  A = 1:1000;<br />
  A([5 982]) = NaN;<br />
  A([7 562]) = Inf;<br />
  A([324 870]) = -Inf;<br />
  B = A.*(A &gt; (17^2)/2);<br />
  all(B == A | B == 0)</p>
<p>Note that not only are elements 5 and 982 of the second B NaN (as you would expect, since any arithmetic operation on a NaN results in a NaN result) but so are elements 324 and 870.  This is because 0*Inf and 0*(-Inf) also both return NaN.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan K</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29106</link>
		<dc:creator>Dan K</dc:creator>
		<pubDate>Fri, 16 May 2008 13:13:19 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29106</guid>
		<description>For myself, generally the answer to my preference depends on what I'm going to do with the selected elements.  If I'm going to be doing something that can be vectorized (is "vectorizable"?) then I try to use logical arrays.  If not, I tend to need the actual indices of the selected elements.  As a related question, is there any way to determine which of the following will be faster (or even a rule of thumb)?
A= rand(10000,100);
A(5001:end,:)=[];
B=A(1:5000,:);
A=A(1:5000,:);
A=A(1:5000,1:100); %Explicityly stating the number of columns
B=zeros(5000,100);
for n=1:size(A,2)
B(:,n)=A(1:5000,n);
end
some other method?

When I'm dealing with very large data sets, this is one of the most frustrating slow points, since it can exceed the time requirements of many of the calculations.
Dan</description>
		<content:encoded><![CDATA[<p>For myself, generally the answer to my preference depends on what I&#8217;m going to do with the selected elements.  If I&#8217;m going to be doing something that can be vectorized (is &#8220;vectorizable&#8221;?) then I try to use logical arrays.  If not, I tend to need the actual indices of the selected elements.  As a related question, is there any way to determine which of the following will be faster (or even a rule of thumb)?<br />
A= rand(10000,100);<br />
A(5001:end,:)=[];<br />
B=A(1:5000,:);<br />
A=A(1:5000,:);<br />
A=A(1:5000,1:100); %Explicityly stating the number of columns<br />
B=zeros(5000,100);<br />
for n=1:size(A,2)<br />
B(:,n)=A(1:5000,n);<br />
end<br />
some other method?</p>
<p>When I&#8217;m dealing with very large data sets, this is one of the most frustrating slow points, since it can exceed the time requirements of many of the calculations.<br />
Dan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Booer</title>
		<link>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29105</link>
		<dc:creator>Tony Booer</dc:creator>
		<pubDate>Fri, 16 May 2008 13:02:39 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/#comment-29105</guid>
		<description>Actually, to answer Loren's original question "Which Method(s) Do You Prefer?", I have to say "none of the above".

I often rely on the rather naughty mix of arithmetic and logical operations:

A .* (A &#62; (17^2)/2)

...because it doesn't require an assignment statement and can be used inline as part of a longer expression.

-Tony</description>
		<content:encoded><![CDATA[<p>Actually, to answer Loren&#8217;s original question &#8220;Which Method(s) Do You Prefer?&#8221;, I have to say &#8220;none of the above&#8221;.</p>
<p>I often rely on the rather naughty mix of arithmetic and logical operations:</p>
<p>A .* (A &gt; (17^2)/2)</p>
<p>&#8230;because it doesn&#8217;t require an assignment statement and can be used inline as part of a longer expression.</p>
<p>-Tony</p>
]]></content:encoded>
	</item>
</channel>
</rss>
