MATLAB one-liners
古いコメントを表示
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?
採用された回答
その他の回答 (9 件)
Matt Tearle
2011 年 3 月 3 日
I'm a huge fan of logical indexing. Expressions like
mean(frogs(wombats > 42))
rock my world.
3 件のコメント
Andrew Newell
2011 年 3 月 3 日
Sean de Wolski
2011 年 3 月 3 日
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
2011 年 3 月 3 日
Sean de Wolski
2011 年 3 月 3 日
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 件のコメント
Sean de Wolski
2011 年 3 月 3 日
idx can usually be a one line expression from cellfun making this a super-awesome-long-one-liner.
Matt Fig
2011 年 3 月 3 日
groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);
Pretty Slick.
6 件のコメント
Andrew Newell
2011 年 3 月 3 日
Matt Fig
2011 年 3 月 3 日
LOL, LI is tops, but not always necessary or useful.
Walter Roberson
2011 年 3 月 3 日
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
2011 年 3 月 3 日
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
2011 年 3 月 3 日
Matt Fig: "LI is tops, but not always necessary or useful"
An unbeliever! Persecute! Kill the heretic!
Walter Roberson
2011 年 3 月 3 日
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
2011 年 3 月 3 日
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.
Andrew Newell
2011 年 3 月 3 日
Andrew Newell
2011 年 3 月 6 日
Oleg Komarov
2011 年 3 月 3 日
eval('fliplr(['''' 33 33 33 33 33 76 105 118 69 32 109 39 73 ''''])')
MuAhahauHAh!!!
3 件のコメント
Andrew Newell
2011 年 3 月 3 日
Oleg Komarov
2011 年 3 月 3 日
You're the evilest!
the cyclist
2011 年 3 月 28 日
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".
Andrew Newell
2011 年 3 月 4 日
3 件のコメント
Walter Roberson
2011 年 3 月 4 日
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
2011 年 3 月 29 日
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
2011 年 3 月 29 日
Drew Weymouth
2011 年 3 月 4 日
0 投票
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 件のコメント
Walter Roberson
2011 年 3 月 4 日
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.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!