Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A = double(pr);
- runs = 10;
- X = 2;
- Y = 4;
- %--------------------------------------------------------------------------
- % JONAS
- tic
- for i = 1:runs
- [nRows, nCols] = size(A);
- [u, v] = find(A); %# find all non-zero elements in A
- %# for each row, find the highest column index with accumarray
- %# and convert to linear index with sub2ind
- lastIdx = sub2ind([nRows, nCols], (1:nRows)', accumarray(u, v, [nRows, 1], @max, NaN));
- goodRows1 = (A(:, 1) == X & A(lastIdx) == Y) | (A(:, 1) == Y & A(lastIdx) == X);
- rows1 = A(goodRows1, :);
- end
- t1 = toc/runs
- %--------------------------------------------------------------------------
- % BRENDAN
- tic
- for i = 1:runs
- % 'a' is your array
- [nx, ny] = size(A);
- inds = [0:ny:ny*(nx-1)]' + sum(A ~= 0, 2);
- % Needs to transpose so that the indexing reads left-to-right
- aT = A';
- goodRows2 = (aT(inds) == X & A(:,1) == Y) | (aT(inds) == Y & A(:,1) == X);
- rows2 = A(goodRows2, :);
- end
- t2 = toc/runs
- %--------------------------------------------------------------------------
- % OLI
- tic
- for i = 1:runs
- lastNumber = sum(A.*[A(:,2:end)==0 true(size(A,1),1)], 2);
- goodRows3 = find((A(:, 1) == X & lastNumber == Y) | (A(:, 1) == Y & lastNumber == X));
- rows3 = A(goodRows3, :);
- end
- t3 = toc/runs
- %--------------------------------------------------------------------------
- t1/t2
- t1/t3
- t2/t3
- % CHECK EQUALITY:
- isequal(rows1, rows2, rows3)
- rows3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement