フィルターのクリア

Creating a sequential order vector from an unknown size vector

1 回表示 (過去 30 日間)
Tim Tuttle
Tim Tuttle 2016 年 8 月 20 日
コメント済み: Tim Tuttle 2016 年 8 月 21 日
I'm creating a program that would take an unknown size vector from the user and create another vector by assigning a number in sequential order for each element in the vector.
For example, the user types in [2 8 4 6] I would like to create a vector that would read [1 2 3 4]. I can not use any for loops. I have tried using the length() and numel() option to start the count, but only get the actual size of the vector. Thank you for any help.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 20 日
a=[2 8 4 6]
out=1:numel(a)
  1 件のコメント
Tim Tuttle
Tim Tuttle 2016 年 8 月 21 日
Thank you for your help. I was trying to use numel(a):1:end without any luck.

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


Star Strider
Star Strider 2016 年 8 月 21 日
If you want it to automatically create both row and column vectors, depending on the dimension of the input vector, this works:
x1 = [2, 8, 4, 6]; % Row Vector
x2 = [2; 8; 4; 6]; % Column Vector
Vseq = @(x) cumsum(ones(size(x)));
v1 = Vseq(x1) % Row Vector
v2 = Vseq(x2) % Column Vector
v1 =
1 2 3 4
v2 =
1
2
3
4

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by