You can use accumarray to get the same output, although the for loop is still about twice as fast as accumarray (for that data). Which method is fastest may depend on the data size. >> accumarray([a',b'], q, [5,5], @(x){x})
ans =
[] [2x1 double] [] [] []
[8] [] [2x1 double] [] []
[] [ 7] [] [2x1 double] []
[] [] [ 6] [] [2x1 double]
[] [] [] [2x1 double] []
Best Answer