create vector from each element it had

9 ビュー (過去 30 日間)
Maya Harel
Maya Harel 2020 年 6 月 9 日
編集済み: Stephen23 2020 年 6 月 9 日
Hi
Lets say I have a vector that goes: [x y z]
Im trying to create, if possible without a loop, the following vec: [1:x, 1:y, 1:z]
only I don't know who x y or z will ne, or how many elemnts will the first vec have
  2 件のコメント
TADA
TADA 2020 年 6 月 9 日
What's wrong with your solution:
[1:x, 1:y, 1:z]
?
Maya Harel
Maya Harel 2020 年 6 月 9 日
the problem is that I don't define how many elements the first vec will have, It will ne detarmined randomly

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

採用された回答

Stephen23
Stephen23 2020 年 6 月 9 日
編集済み: Stephen23 2020 年 6 月 9 日
Method one: nonzeros:
vec = [x,y,z];
tmp = 1:max(vec);
new = nonzeros((tmp(:)<=vec).*tmp(:)).' % requires >=R2016b
Method two: arrayfun:
new = cell2mat(arrayfun(@(n)1:n,vec,'uni',0))
  1 件のコメント
Maya Harel
Maya Harel 2020 年 6 月 9 日
Thank You very much!!!

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

その他の回答 (1 件)

David Hill
David Hill 2020 年 6 月 9 日
編集済み: David Hill 2020 年 6 月 9 日
  1 件のコメント
Maya Harel
Maya Harel 2020 年 6 月 9 日
thank you, but I think I didn't make myself clear, I mean that I want the new vec to go like the following exemple:
oldVec = [3,4,2]
newVec = [1,2,3,1,2,3,4,1,2]

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

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by