Generate a evenly spaced array based on two arrays
3 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have one set of starting and ending points:
s_pt = [1,8,15];
e_pt = [3,12,20];
Now I would like to generate a evenly spaced array based on the corresponding starting and ending points. In this case, there would be three sets of data: (1-3, 8-12, 15-20). The answer would be like:
answer = [1,2,3,8,9,10,11,12,15,16,17,18,19,20]
Are there any functions to generate this kind of data without using for loop?
採用された回答
その他の回答 (2 件)
Image Analyst
2015 年 5 月 1 日
Is this homework? It sounds like homework, so I'll just give a hint for now. Use the colon operator (look it up) or, if you want a somewhat different way, use linspace(). You're going to have to learn the colon operator VERY soon if you want to do anything in MATLAB, so start now.
Hint:
8:12 is equal to an array 8,9,10,11,12. You can literally do the assignment in one single line of code.
6 件のコメント
Image Analyst
2015 年 5 月 1 日
Like Stephen mentioned, and you noted in your comment to him, for loops are not always the slowest approach . You could even speed this up even more if you preallocated space for "out" with the zeros() function.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!