Note, I added an extra line to the original code to element-wise divide valueSum1 by centroidSum1 to make the output of each type of code the same.įinally, I know this isn't strictly an "answer", however I don't have enough reputation to add a comment, and I thought the benchmarking figures were useful to anyone who is learning MATLAB (like myself) and needs some extra motivation to master vectorization. Toc, clear valueSum1 centroidSum1 centroids Partial Vectorization: Elapsed time is 0.630767 seconds.įull Vectorization: Elapsed time is 0.021129 seconds.īenchmarking code here: %// Datasize parameters You can also create a matrix r by appending these two vectors, the vector r2, will be the second row of the matrix.
If you have two row vectors r1 and r2 with n and m number of elements, to create a row vector r of n plus m elements, by appending these vectors, you write. Gave me the following results: Original Code: Elapsed time is 1.327877 seconds. MATLAB allows you to append vectors together to create new vectors. Do something different to each column of a matrix without a loop.-1) Find a subset of a matrix that satisfies a condition, and do something to it. Look for patterns in a vector and do something to them. Vectorizing the calculation makes a huge difference in performance. Find a subset of a matrix that satisfies a condition, and do something to it. A 1 2 3 4 for ii 1:100 number of iterations. For example, consider the following lines of code: for k1:5 A (k) k end. ValueSum1(j, :) = valueSum1(j, :) + X(i, :) ĭisp('- With Proposed Approach') In a loop: SumMatrix pre-allocate if you know how many total elements in array you'll have. One example of when it is desired to create a simple matrix or vector of pre-determined size is when a loop is used to set or modify the vector data. %// Regularise accumulated array and reshape back to a 2D array versionĪcc1_reg2D = ĬentroidSum = histc(labels,1:K) %// Get labels counts as centroid sumsīenchmarking code %// Datasize parametersĭisp('- With Original Approach') %// offsetted at constant intervals from consecutive columnsĪcc1 = accumarray(all_labels(:),X_cut(:)) %// to create all_labels that had same labels within each column and %// Note that accumarray doesn't accept matrices, so we were required %// Accumulate summations within each column based on the labels. across all columns of XĪll_labels = Get truncated X corresponding to all indices matches across all columns %// OR with bsxfun: = Find all labels, i.e.
%// Also, find their locations in that range array, call those as pos %// Find indices in idx that are within range, call them as labels of columns in X for frequent usage later on Here's the implementation - %// Store no.
bsxfun: For calculating linear indices across all columns for matching row indices from idx.MATLAB Following MATLAB function will take a grayscale or an RGB image as input and. This also introduces a technique how one can use accumarray on a 2D matrix along a certain direction, which isn't possible in a straight-forward manner with it. If D is a numeric vector, matrix, or multidimensional array. accumarray: For accumulating summations as done for calulating valueSum.This could be one fully vectorized approach based on.