Matlab matrix operations without loops - matlab

I have an issue with a code performing some array operations. It is getting too slow, because I am using loops. I am trying for some time to optimize this code and to re-write it with less or without loops. Until now unsuccessfully. Can you please help me solve this:
YVal = 1:1:100000;
M_MAX = 1000;
N_MAX = 2000;
clear YTemp
tic
for M=1:1:M_MAX
for N = 1:1:N_MAX
YTemp(M,N) = sum(YVal (N+1:N+M) ) - sum(YVal (1:M) );
end
end
For large N_MAX and M_MAX the execution time of these two loops is very high. How can I optimize this?
Thank you!

Assuming YVal is larger than N_MAX+M_MAX
sum1 = cumsum( YVal(1:(M_MAX+N_MAX)) ); % sum1(M) = sum(YVal(1:M))
If I'm not mistaken, then
sum( YVal( N+1:N+M ) ) = sum1( N + M ) - sum1( N )
And therefore
YTemp( M, N ) = sum1( N + M ) - sum1( N ) - sum1( M )
Using ndgrid
[M N] = ndgrid( 1:M_MAX, 1:N_MAX );
YTemp = sum1( N + M ) - sum1( N ) - sum1( M );
Have I got it right?
EDIT:
Another go without ndgrid
sum1 = cumsum( YVal( 1 : (N_MAX+M_MAX) ) );
YTemp = bsxfun( #minus, ...
bsxfun( #minus, ...
sum1( bsxfun( #plus, 1:N_MAX, (1:M_MAX)' ) ) , sum1 ),...
sum1' );

You should be able to speed it up a little by hoisting the invariant term out of the inner loop, e.g.
for M=1:1:M_MAX
sum2 = sum(YVal(1:M));
for N = 1:1:N_MAX
YTemp(M,N) = sum(YVal(N+1:N+M)) - sum2;
end
end

Related

Matlab Vectorization - none-zero matrix row indices to cell

I am working with Matlab.
I have a binary square matrix. For each row, there is one or more entries of 1. I want to go through each row of this matrix and return the index of those 1s and store them in the entry of a cell.
I was wondering if there is a way to do this without looping over all the rows of this matrix, as for loop is really slow in Matlab.
For example, my matrix
M = 0 1 0
1 0 1
1 1 1
Then eventually, I want something like
A = [2]
[1,3]
[1,2,3]
So A is a cell.
Is there a way to achieve this goal without using for loop, with the aim of calculating the result more quickly?
At the bottom of this answer is some benchmarking code, since you clarified that you're interested in performance rather than arbitrarily avoiding for loops.
In fact, I think for loops are probably the most performant option here. Since the "new" (2015b) JIT engine was introduced (source) for loops are not inherently slow - in fact they are optimised internally.
You can see from the benchmark that the mat2cell option offered by ThomasIsCoding here is very slow...
If we get rid of that line to make the scale clearer, then my splitapply method is fairly slow, obchardon's accumarray option is a bit better, but the fastest (and comparable) options are either using arrayfun (as also suggested by Thomas) or a for loop. Note that arrayfun is basically a for loop in disguise for most use-cases, so this isn't a surprising tie!
I would recommend you use a for loop for increased code readability and the best performance.
Edit:
If we assume that looping is the fastest approach, we can make some optimisations around the find command.
Specifically
Make M logical. As the below plot shows, this can be faster for relatively small M, but slower with the trade-off of type conversion for large M.
Use a logical M to index an array 1:size(M,2) instead of using find. This avoids the slowest part of the loop (the find command) and outweighs the type conversion overhead, making it the quickest option.
Here is my recommendation for best performance:
function A = f_forlooplogicalindexing( M )
M = logical(M);
k = 1:size(M,2);
N = size(M,1);
A = cell(N,1);
for r = 1:N
A{r} = k(M(r,:));
end
end
I've added this to the benchmark below, here is the comparison of loop-style approaches:
Benchmarking code:
rng(904); % Gives OP example for randi([0,1],3)
p = 2:12;
T = NaN( numel(p), 7 );
for ii = p
N = 2^ii;
M = randi([0,1],N);
fprintf( 'N = 2^%.0f = %.0f\n', log2(N), N );
f1 = #()f_arrayfun( M );
f2 = #()f_mat2cell( M );
f3 = #()f_accumarray( M );
f4 = #()f_splitapply( M );
f5 = #()f_forloop( M );
f6 = #()f_forlooplogical( M );
f7 = #()f_forlooplogicalindexing( M );
T(ii, 1) = timeit( f1 );
T(ii, 2) = timeit( f2 );
T(ii, 3) = timeit( f3 );
T(ii, 4) = timeit( f4 );
T(ii, 5) = timeit( f5 );
T(ii, 6) = timeit( f6 );
T(ii, 7) = timeit( f7 );
end
plot( (2.^p).', T(2:end,:) );
legend( {'arrayfun','mat2cell','accumarray','splitapply','for loop',...
'for loop logical', 'for loop logical + indexing'} );
grid on;
xlabel( 'N, where M = random N*N matrix of 1 or 0' );
ylabel( 'Execution time (s)' );
disp( 'Done' );
function A = f_arrayfun( M )
A = arrayfun(#(r) find(M(r,:)),1:size(M,1),'UniformOutput',false);
end
function A = f_mat2cell( M )
[i,j] = find(M.');
A = mat2cell(i,arrayfun(#(r) sum(j==r),min(j):max(j)));
end
function A = f_accumarray( M )
[val,ind] = ind2sub(size(M),find(M.'));
A = accumarray(ind,val,[],#(x) {x});
end
function A = f_splitapply( M )
[r,c] = find(M);
A = splitapply( #(x) {x}, c, r );
end
function A = f_forloop( M )
N = size(M,1);
A = cell(N,1);
for r = 1:N
A{r} = find(M(r,:));
end
end
function A = f_forlooplogical( M )
M = logical(M);
N = size(M,1);
A = cell(N,1);
for r = 1:N
A{r} = find(M(r,:));
end
end
function A = f_forlooplogicalindexing( M )
M = logical(M);
k = 1:size(M,2);
N = size(M,1);
A = cell(N,1);
for r = 1:N
A{r} = k(M(r,:));
end
end
You can try arrayfun like below, which sweep through rows of M
A = arrayfun(#(r) find(M(r,:)),1:size(M,1),'UniformOutput',false)
A =
{
[1,1] = 2
[1,2] =
1 3
[1,3] =
1 2 3
}
or (a slower approach by mat2cell)
[i,j] = find(M.');
A = mat2cell(i,arrayfun(#(r) sum(j==r),min(j):max(j)))
A =
{
[1,1] = 2
[2,1] =
1
3
[3,1] =
1
2
3
}
Using accumarray:
M = [0 1 0
1 0 1
1 1 1];
[val,ind] = find(M.');
A = accumarray(ind,val,[],#(x) {x});
You can use strfind :
A = strfind(cellstr(char(M)), char(1));
Edit: I added a benchmark, the results show that a for loop is more efficient than accumarray.
You can usefind and accumarray:
[c, r] = find(A');
C = accumarray(r, c, [], #(v) {v'});
The matrix is transposed (A') because find groups by column.
Example:
A = [1 0 0 1 0
0 1 0 0 0
0 0 1 1 0
1 0 1 0 1];
% Find nonzero rows and colums
[c, r] = find(A');
% Group row indices for each columns
C = accumarray(r, c, [], #(v) {v'});
% Display cell array contents
celldisp(C)
Output:
C{1} =
1 4
C{2} =
2
C{3} =
3 4
C{4} =
1 3 5
Benchmark:
m = 10000;
n = 10000;
A = randi([0 1], m,n);
disp('accumarray:')
tic
[c, r] = find(A');
C = accumarray(r, c, [], #(v) {v'});
toc
disp(' ')
disp('For loop:')
tic
C = cell([size(A,1) 1]);
for i = 1:size(A,1)
C{i} = find(A(i,:));
end
toc
Result:
accumarray:
Elapsed time is 2.407773 seconds.
For loop:
Elapsed time is 1.671387 seconds.
A for loop is more efficient than accumarray...

Image Transformation Without Loop

I am writing image transformation function in matlab instead of using predefined functions like 'imwarp or imtransform'. I am done with writing the code roughly but it is taking too long due to the use of loop.
Please suggest me a solution which can achieve the same result without loops
function I2 = f_transform(A, I1)
%A is an affine transformation matrix
%I1 is original image to be transformed
s=size(A);
if(s(1)~=3 || s(2)~=3)
disp("Invalid Transforming Matrix");
I2=0;
return;
end
if(det(A)==0)
disp("The given Transforming Matrix is Singular");
I2=0;
return;
end
[r, c]=size(I1);
extremeValues = [1 1 r r ; 1 c 1 c; 1 1 1 1];
newExtremeValues = A * extremeValues;
minRow = floor( min( newExtremeValues(1,:) ) );
maxRow = ceil( max( newExtremeValues(1,:) ) );
minCol = floor( min( newExtremeValues(2,:) ) );
maxCol = ceil( max( newExtremeValues(2,:) ) );
I2 = zeros(maxRow - minRow + 1, maxCol - minCol + 1);
for i = minRow:maxRow
for j=minCol:maxCol
b = A \ [i j 1]';
p = b(1) / b(3);
q = b(2) / b(3);
if(p >= 1 && p <= r && q >= 1 && q <= c)
I2(i - minRow + 1, j - minCol + 1) = I1(round(p),round(q));
end
end
end
I2 = uint8(I2);
return;
end

I need help for Matlab vectorization of this code

I am new on MatLAB, i don't know proper basics of vectorization..
I am trying to vectorize this function.
function indc = PatchSearch(X, row, col, off, nv, S, I)
[N M] = size(I);
f2 = size(X,2);
rmin = max( row-S, 1 );
rmax = min( row+S, N );
cmin = max( col-S, 1 );
cmax = min( col+S, M );
idx = I(rmin:rmax, cmin:cmax);
idx = idx(:);
B = X(idx, :);
v = X(off, :);
dis = (B(:,1) - v(1)).^2;
for k = 2:f2
dis = dis + (B(:,k) - v(k)).^2;
end
dis = dis./f2;
[val,ind] = sort(dis);
indc = idx( ind(1:nv) );
%indc = idx(dis<250);
I need help from some experts for vectorizating this function
Thanks
You can replace the following loopy portion of your code -
dis = (B(:,1) - v(1)).^2;
for k = 2:f2
dis = dis + (B(:,k) - v(k)).^2;
end
With this bsxfun implementation -
dis = sum(bsxfun(#minus,B,v).^2,2);
The assumption here is that f2 is the number of columns in B, which is the same as the number of elements in v and looking at your code the way B and v are initialized, that seems to be quite right.
One possible solution using repmat and sum:
Replace the loop and the line above it with this code.
dis=sum((B-repmat(v,size(B,1),1)).^2,2);

Setting certain column value of each row in matlab

So I have a 150 X 4 matrix called Z.
I do some operation to this and what I have left is Z again.
[o p] = min(Z, [], 2);
Gives me the column number for each row.
I need to set that particular column for each row to 1 and rest to zero.
I thought of doing this but it didn't work out.
K = zeros(size(Z));
K(:, p) = 1;
You can go through sparse:
K = full( sparse( 1:size(Z,1), p.', 1, size(Z,1), size(Z,2) ) );
Alternatively, you can use sub2ind:
K = zeros( size(Z) );
K( sub2ind( size(K), 1:size(K,1), p.' ) ) = 1;
If you are certain there is only one maximal element in each row you can
K = bsxfun( #eq, Z, o );
One approach -
K = zeros(size(Z))
K((p-1)*size(Z,1)+[1:size(Z,1)]')=1

how to optimize nested for loop in matlab

I have a for loop nested thrice in a matlab program. Can any of you help me optimize it.
w=5;
a = rand(m*n,10); b=rand(m,n);
for i = 1 : m
for j = 1 : n
for k = 1 : l
if (i-w >= 1 && i+w <= m)
featureL = a(((i-1)*n)+j,:); featureR = a(((i-1)*n)+j-d,:);
D1(i,j,k) = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
D2(i,j,k) = mean2( b(i-w:i+w, j-w:j+w) );
end
end
end
end
I know the performance could be heavily improved by using meshgrid, but I am not sure how to do it.
Thanks in anticipation.
Can it be done something like this..
[X Y Z] = meshgrid(1:m,1:n,1:l);
D1(something containing X,Y,Z) = sqrt( sum( ( a(something cont. X,Y) - a(something cont. X,Y)).*(a(something cont. X,Y) - a(something cont. X,Y)) ) );
% similarly D2
Thanks a lot!.
I've found that a good way to attack these things is incrementally. Start by examining everything in the innermost loop, and see if it can be done at a higher level. This will reduce repeated computations.
For example, you can perform your if (i-w >= 1 && i+w <= m) two levels higher (since it only depends on i,w, and m), reducing if checks and skipping loop iterations.
Once that is done, your featureL and featureR calculations can be moved up one level; they are performed inside the k loop but only depend on j. Similarly, sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) ) can be computed outside of the k loop, put into a variable, and assigned later.
In fact, as far as I can see you can get rid of the entire k loop since k is never used. Here's your code with some of this applied:
w=5;
a = rand(m*n,10);
b=rand(m,n);
for i = 1 : m
if (i-w >= 1 && i+w <= m)
for j = 1 : n
featureL = a(((i-1)*n)+j,:);
featureR = a(((i-1)*n)+j-d,:);
x = sqrt( sum( (featureL - featureR) .* (featureL - featureR) ) );
y = mean2( b(i-w:i+w, j-w:j+w) )
D1(i,j,:) = x;
D2(i,j,:) = y;
end
end
end