arithmetic/logical combination, that will work … as long as all your elements of A are finite.
A = 1:1000;
B = A.*(A > (17^2)/2);
all(B == A | B == 0)
A = 1:1000;
A([5 982]) = NaN;
A([7 562]) = Inf;
A([324 870]) = -Inf;
B = A.*(A > (17^2)/2);
all(B == A | 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.
Nice reply to Tony’s challenge. You can use end’s in the expressions of your first solutions as well:
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));
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.
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.
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 ?
Here’s the documentation for end:
The capability has been allowed, I believe, since its introduction as an index (and overloadable with the class system).
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 > 0.5; toc
Elapsed time is 4.771542 seconds.
tic; i = find(l); toc
Elapsed time is 29.321260 seconds.
>> tic; y=x(l); toc
Elapsed time is 49.782177 seconds.
>> 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);
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 subsc