transform a vector to a cell array

Hi,
c=[5 7 1 9 10]; I want to create a cell from this vector as:
d={[5 7 1] [7 1 9] [1 9 10]};
Thanks in advance.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 29 日
編集済み: Azzi Abdelmalek 2013 年 12 月 29 日

0 投票

c=[5 7 1 9 10];
m=3;
out=arrayfun(@(x) c(x:x+m-1),1:numel(c)-m+1,'un',0)

4 件のコメント

allnab
allnab 2013 年 12 月 30 日
hi, it does not give the desired result {out} cell
allnab
allnab 2013 年 12 月 30 日
{out} cell
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 30 日
What do you mean? The result is exactly what you asked for. to see the result type
celldisp(out)
allnab
allnab 2013 年 12 月 30 日
Excuse me it works correctly now
thank you

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

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 29 日

0 投票

d = num2cell(c(hankel(1:3,3:numel(c))),2)

3 件のコメント

allnab
allnab 2013 年 12 月 30 日
hi, error message:
??? Attempted to access r(1); index out of bounds because numel(r)=0.
Error in ==> hankel at 26 elseif c(nc) ~= r(1)
Jos (10584)
Jos (10584) 2013 年 12 月 30 日
This error is shown when the second input to hanker is empty:
hankel(1:3,[])
In the code above this occurs because,
  • 3:numel(c) is empty, because,
  • numel(c) is less then 3, so,
check the contents of c !
allnab
allnab 2013 年 12 月 30 日
Excuse me it works correctly now
thank you

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

2013 年 12 月 29 日

コメント済み:

2013 年 12 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by