avoiding nested for loops in MATLAB - matlab

I have a scenario in MATLAB where I would like to evaluate a function for certain parameter values. The parameters are extracted from an arbitrary number of arrays, and each array can have an arbitrary number of elements. I know the number of arrays and the number of elements in them before I call the function.
For example, say I have arrays A = [a1 a2 ... aL], B = [b1 b2 ... bM] and C = [c1 c2 ... cN].
for i = 1:length(A)
for j = 1:length(B)
for k = 1:length(C)
myfunc(A(i), B(j), C(k))
end
end
end
I am considering taking the L elements of A, M elements of B and N elements of C, and flatenning them into a cell array, and iterating with a single for loop over this cell array.
I was wondering if there was a MATLAB function that does something like this ... The result does not have to be a cell array. I want a way to avoid having multiple nested for loops. It is fine for a small number of loops, but as this number grows, it is pretty difficult to read and maintain.

ndgrid can be used to flatten several nested loops into one. It generates all combinations of values (variables aa, bb, cc in the code below), so that a single index (k below) can be used to traverse all combinations. But note that generating all combinations may required a lot of memory, depending on your L, M, N.
[cc, bb, aa] = ndgrid(C, B, A); %// outermost variable should be rightmost here
for k = 1:numel(aa)
myfunc(aa(k), bb(k), cc(k));
end

Thanks to direction from the accepted answer, I made a function that generalizes to any number of arrays. The result is a 2D array of N-tuples - where N is the number of input arrays.
function [Result] = makeTuples(varargin)
nInputArgs = length(varargin);
b = cell(1, nInputArgs);
a = flip(varargin);
[b{:}] = ndgrid(a{:});
bb = flip(b);
nOutputs = length(bb);
N = numel(bb{1});
Result = zeros(N, nInputArgs);
arr = zeros(1,nInputArgs);
for j = 1:N
for k = 1:nOutputs
arr(k) = bb{k}(j);
end
Result(j,:) = arr;
arr = zeros(1,nInputArgs);
end
end

Related

Fastest way of finding the only index of vector b where array A(i,j) == b

I have 2 big arrays A and b:
A: 10.000++ rows, 4 columns, not unique integers
b: vector with 500.000++ elements, unique integers
Due to the uniqueness of the values of b, I need to find the only index of b, where A(i,j) == b.
What I started with is
[rows,columns] = size(A);
B = zeros(rows,columns);
for i = 1 : rows
for j = 1 : columns
B(i,j) = find(A(i,j)==b,1);
end
end
This takes approx 5.5 seconds to compute, which is way to long, since A and b can be significantly bigger... That in mind I tried to speed up the code by using logical indexing and reducing the for-loops
[rows,columns] = size(A);
B = zeros(rows,columns);
for idx = 1 : numel(b)
B(A==b(idx)) = idx;
end
Sadly this takes even longer: 21 seconds
I even tried to do use bsxfun
for i = 1 : columns
[I,J] = find(bsxfun(#eq,A(:,i),b))
... stitch B together ...
end
but with a bigger arrays the maximum array size is quickly exceeded (102,9GB...).
Can you help me find a faster solution to this? Thanks in advance!
EDIT: I extended find(A(i,j)==b,1), which speeds up the algorithm by factor 2! Thank you, but overall still too slow... ;)
The function ismember is the right tool for this:
[~,B] = ismember(A,b);
Test code:
function so
A = rand(1000,4);
b = unique([A(:);rand(2000,1)]);
B1 = op1(A,b);
B2 = op2(A,b);
isequal(B1,B2)
tic;op1(A,b);op1(A,b);op1(A,b);op1(A,b);toc
tic;op2(A,b);op2(A,b);op2(A,b);op2(A,b);toc
end
function B = op1(A,b)
B = zeros(size(A));
for i = 1:numel(A)
B(i) = find(A(i)==b,1);
end
end
function B = op2(A,b)
[~,B] = ismember(A,b);
end
I ran this on Octave, which is not as fast with loops as MATLAB. It also doesn't have the timeit function, hence the crappy timing using tic/toc (sorry for that). In Octave, op2 is more than 100 times faster than op1. Timings will be different in MATLAB, but ismember should still be the fastest option. (Note I also replaced your double loop with a single loop, this is the same but simpler and probably faster.)
If you want to repeatedly do the search in b, it is worthwhile to sort b first, and implement your own binary search. This will avoid the checks and sorting that ismember does. See this other question.
Assuming that you have positive integers you can use array indexing:
mm = max(max(A(:)),max(b(:)));
idxs = sparse(b,1,1:numel(b),mm,1);
result = full(idxs(A));
If the range of values is small you can use dense matrix instead of sparse matrix:
mm = max(max(A(:)),max(b(:)));
idx = zeros(mm,1);
idx(b)=1:numel(b);
result = idx(A);

Changing elements of two matrix in a condition matlab

Consider two n by n-1 matrix and an n by 1 vector (for example lets call them in order A, B and v). Elements of v are zero or one. If element v(m,1) is equal to one, I want to replace elements A(1:m-1,m-1) by B(1:m-1,m-1) and elements A(m+1:n,m) by B(m+1:n,m).
How can I do that? Could anyone help? To make the question more clear, consider below example.
example:
A=[1,2,3;4,5,6;7,8,9;12,13,14]
B=[3,4,5;6,7,8;9,10,11;6,5,3]
v=[0,1,0,1]
Result should be:
result= [3,2,5;4,5,8;7,10,11;12,5,14]
Here is an alternative, using logical indexing:
temp = A;
ind = 1:size(v,2);
for k = ind(v==1)
if k<=size(A,2)+1
A(1:k-1,k-1) = B(1:k-1,k-1);
B(1:k-1,k-1) = temp(1:k-1,k-1);
if k<size(A,2)
A(k+1:end,k) = B(k+1:end,k);
B(k+1:end,k) = temp(k+1:end,k);
end
end
end
You can acheive the desired result using find, which returns the indexes on non-zero elements, and a for-loop:
R = A; % assuming you've set A, B and v already.
n = size(A,1);
v1 = find(v);
for i=1:length(v1)
m=v1(i);
if m>1
R(1:m-1,m-1)=B(1:m-1,m-1);
end
if m<n
R(m+1:end,m)=B(m+1:end,m);
end
end
As I point out in a comment, v has to have length n-1 if v(n-1)=1 otherwise m+1:end is not a valid index range.
Edited to make second assignment optionally as per comments.

Extract elements of matrix from a starting element and size

Sorry about the bad title, I'm struggling to word this question well. Basically what I want to do is extract elements from a 2d matrix, from row by row, taking out a number of elements (N) starting at a particular column (k). In for loops, this would look like.
A = magic(6);
k = [2,2,3,3,4,4]; % for example
N = 3;
for j = 1:length(A)
B(j,:) = A(j,k(j):k(j)+N-1);
end
I figure there must be a neater way to do it than that.
You could use bsxfun to create an array of indices to use. Then combine this with the row numbers and pass it to sub2ind.
inds = sub2ind(size(A), repmat(1:size(A, 1), 3, 1), bsxfun(#plus, k, (0:(N-1))')).';
B = A(inds);
Or alternately without sub2ind (but slightly more cryptic).
B = A(bsxfun(#plus, 1:size(A,1), ((bsxfun(#plus, k, (0:(N-1)).')-1) * size(A,1))).');
Here's one approach using bsxfun's masking capability and thus logical indexing -
C = (1:size(A,2))';
At = A.';
B = reshape(At(bsxfun(#ge,C,k) & bsxfun(#lt,C,k+N)),N,[]).';

Best way to count all elements in a cell array?

I want to count all elements in a cell array, including those in "nested" cells.
For a cell array
>> C = {{{1,2},{3,4,5}},{{{6},{7},{8}},{9}},10}
C = {1x2 cell} {1x2 cell} [10]
The answer should be 10.
One way is to use [C{:}] repeatedly until there are no cells left and then use numel but there must be a better way?
Since you are only interested in the number of elements, here is a simplified version of flatten.m that #Ansari linked to:
function n = my_numel(A)
n = 0;
for i=1:numel(A)
if iscell(A{i})
n = n + my_numel(A{i});
else
n = n + numel(A{i});
end
end
end
The result:
>> C = {{{1,2},{3,4,5}},{{{6},{7},{8}},{9}},10};
>> my_numel(C)
ans =
10
EDIT:
If you are feeling lazy, we can let CELLPLOT do the counting:
hFig = figure('Visible','off');
num = numel( findobj(cellplot(C),'type','text') );
close(hFig)
Basically we create an invisible figure, plot the cell array, count how many "text" objects were created, then delete the invisible figure.
This is how the plot looks like underneath:
Put this in a function (say flatten.m) (code from MATLAB Central):
function C = flatten(A)
C = {};
for i=1:numel(A)
if(~iscell(A{i}))
C = [C,A{i}];
else
Ctemp = flatten(A{i});
C = [C,Ctemp{:}];
end
end
Then do numel(flatten(C)) to find the total number of elements.
If you don't like making a separate function, you can employ this clever (but nasty) piece of code for defining a flatten function using anonymous functions (code from here):
flatten = #(nested) feval(Y(#(flat) #(v) foldr(#(x, y) ifthenelse(iscell(x) | iscell(y), #() [flat(x), flat(y)], #() {x, y}), [], v)), nested);
Either way, you need to recurse to flatten the cell array then count.

split a list into several variables in matlab

If I have a short list (let's say two or three elements) I would like to have function that split it in several variables. Something like that:
li=[42 43];
[a b]=split(li)
--> a=42
--> b=43
I am looking for some way to make my code shorter in matlab. This one would be nice in some situations For instance:
positions=zeros(10,3);
positions= [....];
[x y z]=split(max(positions,1))
instead of doing:
pos=max(positions,1)
x=pos(1);
y=pos(2);
z=pos(3);
The only way I know of to do this is to use deal. However this works with cell arrays only, or explicit arguments in to deal. So if you want to deal matrices/vectors, you have to convert to a cell array first with num2cell/mat2cell. E.g.:
% multiple inputs
[a b] = deal(42,43) % a=2, b=3
[x y z] = deal( zeros(10,1), zeros(10,1), zeros(10,1) )
% vector input
li = [42 43];
lic = num2cell(li);
[a b]=deal(lic{:}) % unforunately can't do num2cell(li){:}
% a=42, b=43
% matrix input
positions = zeros(10,3);
% create cell array, one column each
positionsc = mat2cell(positions,10,[1 1 1]);
[x y z] = deal(positionsc{:})
The first form is nice (deal(x,y,...)) since it doesn't require you to explicitly make the cell array.
Otherwise I reckon it's not worth using deal when you have to convert your matrices to cell arrays only to convert them back again: just save the overhead. In any case, it still takes 3 lines: first to define the matrix (e.g. li), then to convert to cell (lic), then to do the deal (deal(lic{:})).
If you really wanted to reduce the number of lines there's a solution in this question where you just make your own function to do it, repeated here, and modified such that you can define an axis to split over:
function varargout = split(x,axis)
% return matrix elements as separate output arguments
% optionally can specify an axis to split along (1-based).
% example: [a1,a2,a3,a4] = split(1:4)
% example: [x,y,z] = split(zeros(10,3),2)
if nargin < 2
axis = 2; % split along cols by default
end
dims=num2cell(size(x));
dims{axis}=ones([1 dims{axis}]);
varargout = mat2cell(x,dims{:});
end
Then use like this:
[a b] = split([41 42])
[x y z]= split(zeros(10,3), 2) % each is a 10x1 vector
[d e] = split(zeros(2,5), 1) % each is a 1x5 vector
It still does the matrix -> cell -> matrix thing though. If your vectors are small and you don't do it within a loop a million times you should be fine though.
You can extract the values in the list into different variables with
li = [42 43 44];
tmp = num2cell(li);
[a b c] = deal(tmp{:})
a =
42
b =
43
c =
44
You can manage this in a single expression if you really wanted to:
a = [1, 2, 3, 4]
[b, c, d, e]=feval(#(x)x{:}, num2cell(a))
Result:
b =
1
c =
2
d =
3
e =
4
If you wanted to use this a lot you could quickly make an anonymous function:
expand = #(A) feval(#(x)x{:}, num2cell(A))
[b, c, d, e]=expand(a)
I believe this would work on anything that num2cell will accept, which doesn't even have to be numeric, just needs to be an array, which almost everything in Matlab is.