clear all,clc
A
= rand([300,300,300]); % Generate random matrix
[M, Ind] = max(A,[],2); % Take the max along dimension 2
num_runs = 5;
for k = 1:50000
tic(); elapsed = toc(); %// Warm up tic/toc
end
tic
for iter = 1:num_runs
[m,n,o] = size(A);
dim1 = mod((0:m*o-1)', m)+1;
dim2 = Ind(:);
dim3 = ceil((1:m*o)/m)';
ind = sub2ind(size(A), dim1, dim2, dim3);
end
toc
clear dim1 dim2 ind dim3 m n o
tic
for iter = 1:num_runs
[n1,n2,n3] = size(A); %// Get the size of A
idx = bsxfun(@plus,bsxfun(@plus,squeeze((Ind-1)*n1),[0:n3-1]*n1*n2),[1:n1]');
idx = idx(:);
end
toc
clear idx n1 n2 n3
tic
for iter = 1:num_runs
[m, n, p] = size(A);
[M, Ind] = max(A,[],2);
LinInd = bsxfun(@plus, (1:m).', (0:p-1)*m*n); %'//
LinInd = LinInd(:) + (Ind(:)-1)*m;
end
toc
Y2xlYXIgYWxsLGNsYwoKQSA9IHJhbmQoWzMwMCwzMDAsMzAwXSk7ICAgICAlIEdlbmVyYXRlIHJhbmRvbSBtYXRyaXgKQiA9IHJhbmQoc2l6ZShBKSk7CltNLCBJbmRdID0gbWF4KEEsW10sMik7ICAgJSBUYWtlIHRoZSBtYXggYWxvbmcgZGltZW5zaW9uIDIKbnVtX3J1bnMgPSA1OwoKZm9yIGsgPSAxOjUwMDAwCiAgICB0aWMoKTsgZWxhcHNlZCA9IHRvYygpOyAlLy8gV2FybSB1cCB0aWMvdG9jCmVuZAoKdGljCmZvciBpdGVyID0gMTpudW1fcnVucwogICAgW20sbixvXSA9IHNpemUoQSk7CiAgICAKICAgIGRpbTEgPSBtb2QoKDA6bSpvLTEpJywgbSkrMTsKICAgIGRpbTIgPSBJbmQoOik7CiAgICBkaW0zID0gY2VpbCgoMTptKm8pL20pJzsKICAgIAogICAgaW5kID0gc3ViMmluZChzaXplKEEpLCBkaW0xLCBkaW0yLCBkaW0zKTsKZW5kCnRvYwpjbGVhciBkaW0xIGR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