To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016
One of the joys of using MATLAB is that it has a stock of matrix functions like diff, sort, all, and so on, that can be combined in all sorts of interesting ways. For example, in a recent question, the challenge was to find a compact code to determine which columns of a matrix A have all elements equal. Matt Tearle came up with this nifty answer:
What are your favorite one-line MATLAB expressions?
cell2mat(arrayfun(@(K) accumarray(C, F(:,K), , @mean), 1:size(F,2), 'Uniform', 0))
In response to a cssm question:
I have a feature matrix, F(m, n) and a cluster vector, C(m, 1). Now I want to get the mean of feature in F according to C. Make it simple as below:
F = [2 5; 3 7; 8 4] C = [2; 1; 2] output should be [3 7; (2+8)/2 (5+4)/2] =[3 7; 5 4.5]
I'm a huge fan of logical indexing. Expressions like
mean(frogs(wombats > 42))
rock my world.
Here's another from a thread today:
Given a connected components analysis (bwconncomp) and some criteria for objects to meet: remove objects that don't meet that criteria from your binary image:
I(cell2mat(CC.PixelIdxList(~idx)')) = false;
Here is a good one. After already writing a solution to this Question, I stumbled upon this:
groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);
Inspired by something I'm working on right now & your comment to my previous answer...
If you have an n-by-1 structure array people with a field suck (which contains a scalar for each struct element), and you want to find the average:
Extract multiple elements, concatenate, apply function. All in one line.
Turn a structure array S into a cell array with the names of the fields in the first column:
C = horzcat(fieldnames(S), squeeze(struct2cell(S)))
Here is a real beauty from a comment by Tim Davis on a guest blog for Loren Shure. Suppose you have three 2D vectors p1, p2 and p3, and you want to know if they are collinear. The shortest solution also happens to be numerically the most reliable:
rank ([p2-p1 ; p3-p1]) < 2
It is also easily generalized to more dimensions!
eval('fliplr(['''' 33 33 33 33 33 76 105 118 69 32 109 39 73 ''''])')
Here is an interesting way of calculating n rows of Pascal's triangle:
Now if we could just remove the zeros and center it on the same line!