MATLAB one-liners
26 views (last 30 days)
Show older comments
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:
all(~diff(A))
What are your favorite one-line MATLAB expressions?
0 Comments
Accepted Answer
Walter Roberson
on 3 Mar 2011
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]
More Answers (9)
Matt Tearle
on 3 Mar 2011
I'm a huge fan of logical indexing. Expressions like
mean(frogs(wombats > 42))
rock my world.
Sean de Wolski
on 3 Mar 2011
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;
1 Comment
Sean de Wolski
on 3 Mar 2011
idx can usually be a one line expression from cellfun making this a super-awesome-long-one-liner.
Matt Fig
on 3 Mar 2011
groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);
Pretty Slick.
6 Comments
Walter Roberson
on 3 Mar 2011
Ah, I found my copy, and it was _not_ a 1 liner. I had used
b=diff(a); %find differences
idx = find([b 2]>1); %find their indexes
cel = mat2cell(a, 1, [idx(1) diff(idx)]); %break up the matrix
Matt Tearle
on 3 Mar 2011
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:
mean([people.suck])
Extract multiple elements, concatenate, apply function. All in one line.
Oleg Komarov
on 3 Mar 2011
eval('fliplr(['''' 33 33 33 33 33 76 105 118 69 32 109 39 73 ''''])')
MuAhahauHAh!!!
3 Comments
the cyclist
on 28 Mar 2011
I used to be an admin on a chess server where "qu" could be used as a shorthand to quit out of the interface. A common prank was to tell newbies that "qu" could be used to display the "quote of the day".
Drew Weymouth
on 4 Mar 2011
Read in an image and convert it to a grayscale, double matrix of data range 0..1
im= rgb2gray(double(imread('filename.jpg'))/255);
1 Comment
Walter Roberson
on 4 Mar 2011
or more generally, rgb2gray(im2double(imread('filename.jpg')))
Your code would fail for images that happened to be already double or happened to be uint16.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!