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?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Mär. 2011

3 Stimmen

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]

2 Kommentare

Matt Fig
Matt Fig am 3 Mär. 2011
ARRAYFUN, and ACUMARRAY? About all that is missing is to somehow work BSXFUN in there too!
Andrew Newell
Andrew Newell am 28 Mär. 2011
Even though BSXFUN is missing, I decided to accept this one. Very clever, @Walter!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (9)

Matt Tearle
Matt Tearle am 3 Mär. 2011

1 Stimme

I'm a huge fan of logical indexing. Expressions like
mean(frogs(wombats > 42))
rock my world.

3 Kommentare

Andrew Newell
Andrew Newell am 3 Mär. 2011
Or how about mean(people(suck))?
Sean de Wolski
Sean de Wolski am 3 Mär. 2011
I use the function 'keep' from the FEX which performs the inverse of clear.
I also use the variables 'in', 'out' a lot. So the other day I typed:
'keep out'
which kind of made my day.
Andrew Newell
Andrew Newell am 3 Mär. 2011
I like it!

Melden Sie sich an, um zu kommentieren.

Sean de Wolski
Sean de Wolski am 3 Mär. 2011

1 Stimme

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 Kommentar

Sean de Wolski
Sean de Wolski am 3 Mär. 2011
idx can usually be a one line expression from cellfun making this a super-awesome-long-one-liner.

Melden Sie sich an, um zu kommentieren.

Matt Fig
Matt Fig am 3 Mär. 2011

1 Stimme

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);
Pretty Slick.

6 Kommentare

Andrew Newell
Andrew Newell am 3 Mär. 2011
Very nice, but Matt would be happier if you used logical indexing.
Matt Fig
Matt Fig am 3 Mär. 2011
LOL, LI is tops, but not always necessary or useful.
Walter Roberson
Walter Roberson am 3 Mär. 2011
Strange, I am sure I wrote code much like that in response to one of the numerous copies of that question. I cannot seem to find it now, though. I had the 1 first, and I used > instead of ~=
Matt Fig
Matt Fig am 3 Mär. 2011
I had it stored in my "One liners" file, which is made up of one-liners either I or somebody else on CSSM wrote in response to some question. The file is too big to be much use anymore...
Matt Tearle
Matt Tearle am 3 Mär. 2011
Matt Fig: "LI is tops, but not always necessary or useful"
An unbeliever! Persecute! Kill the heretic!
Walter Roberson
Walter Roberson am 3 Mär. 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

Melden Sie sich an, um zu kommentieren.

Matt Tearle
Matt Tearle am 3 Mär. 2011

1 Stimme

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.

1 Kommentar

Andrew Newell
Andrew Newell am 3 Mär. 2011
I like compact expressions with social commentary.

Melden Sie sich an, um zu kommentieren.

Andrew Newell
Andrew Newell am 3 Mär. 2011

1 Stimme

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)))
Andrew Newell
Andrew Newell am 6 Mär. 2011

1 Stimme

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!
Oleg Komarov
Oleg Komarov am 3 Mär. 2011

0 Stimmen

eval('fliplr(['''' 33 33 33 33 33 76 105 118 69 32 109 39 73 ''''])')
MuAhahauHAh!!!

3 Kommentare

Andrew Newell
Andrew Newell am 3 Mär. 2011
If you were really evil (or a quitter), you'd do
eval(char([113 117 105 116]))
Oleg Komarov
Oleg Komarov am 3 Mär. 2011
You're the evilest!
the cyclist
the cyclist am 28 Mär. 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".

Melden Sie sich an, um zu kommentieren.

Andrew Newell
Andrew Newell am 4 Mär. 2011

0 Stimmen

Here is an interesting way of calculating n rows of Pascal's triangle:
round(expm(diag(1:n-1,-1)))
Now if we could just remove the zeros and center it on the same line!

3 Kommentare

Walter Roberson
Walter Roberson am 4 Mär. 2011
sprintf() it and regexrep() on the result, substituting spaces for leading space-zero-space; another regexprep() call could substitute spaces for trailing space-zero-space.
Doug Eastman
Doug Eastman am 29 Mär. 2011
It's not pretty but just for fun, here's one way to do it:
trimmedTriangle = cell2mat(cellfun(@(x) x(1:size(num2str(expm(diag(1:n-1,-1))),2)),cellfun(@(x,y)[x y],cellfun(@(x) repmat(' ',1,x),num2cell(round(linspace(size(num2str(expm(diag(1:n-1,-1))),2)/2,0,n))'),'UniformOutput',false),regexprep(mat2cell(num2str(expm(diag(1:n-1,-1))),ones(n,1)),' 0',' '),'UniformOutput',false),'UniformOutput',false))
Andrew Newell
Andrew Newell am 29 Mär. 2011
Wow! Three nested cellfuns! This deserves a separate answer.

Melden Sie sich an, um zu kommentieren.

Drew Weymouth
Drew Weymouth am 4 Mär. 2011

0 Stimmen

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 Kommentar

Walter Roberson
Walter Roberson am 4 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by