フィルターのクリア

Is there something like 'array colon'? in other words how can I apply colon operator to all the elements of an array (please read the text below so my question will make sense)?

1 回表示 (過去 30 日間)
A = [1 10 16 23];
A-3:A+2 will result in [-2,-1,0,1,2,3]; In other words colon operates only on the first element of array A. However I want to apply A-3:A+2 on all the elements of A; I know I can get this by a loop, like :
u=[];for i=1:4, u=[u,A(i)-3:A(i)+2]; end
However I wonder if there is a way to do this without using loop. (The actual array I am working with is too long to loop through it.)
This sounds like a super simple question but I have no ides how to do it in a simple way (other than looping).
I so much appreciate any help.

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 28 日
A = [1 10 16 23];
u = reshape(bsxfun(@plus,A,(-3:2).'),1,[])

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 28 日
編集済み: Azzi Abdelmalek 2012 年 10 月 28 日
do you mean a division
A = [1 10 15 23];
(A-3)./(A+2 )
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 28 日
編集済み: Azzi Abdelmalek 2012 年 10 月 28 日
I'm not sur what you need
A = [1 10 15 23];
unique(cell2mat(arrayfun(@(x) x-3:x+2,A,'un',0)))

サインインしてコメントする。


Jan
Jan 2012 年 10 月 28 日
  2 件のコメント
Farzaneh
Farzaneh 2012 年 10 月 28 日
When I use mcolon , it gives me this error:
Undefined function 'getclassidmex' for input arguments of type 'double'.
Error in castarrays (line 16) id = getclassidmex(varargout{:}); % get id from 1-12, ordered like numclass
Error in mcolon (line 50) [i1,d,i2] = castarrays(i1(:),d(:),i2(:));
Matt Fig
Matt Fig 2012 年 10 月 28 日
MCOLON is a mex function that must be compiled. You don't need MCOLON to do what you want to do here anyway. See my solution.

サインインしてコメントする。


Chris A
Chris A 2012 年 10 月 28 日
編集済み: Chris A 2012 年 10 月 28 日
Here is one way to do this:
A = [1 10 16 23];
res=reshape(repmat((-3:2)',1,numel(A))+repmat(A,6,1),[],1);
  1 件のコメント
Farzaneh
Farzaneh 2012 年 10 月 28 日
Thanks. It is similar to what Matt Fig suggested. however he suggested bsxfun which makes things easier than using repmat...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by