Generate a evenly spaced array based on two arrays

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?

댓글 수: 3

why do you not want a for loop? it would be easy to accomplish in 2~3 lines
Because it takes more time and occupied more memory with for loop.
Guillaume
Guillaume 2015년 5월 1일
편집: Guillaume 2015년 5월 1일
This is actually something I submitted as a cody problem a while back.
And this is the reverse problem I also submitted.
If you want to see all the solutions you would have to submit a valid solution (of any size) in the first place and then solve any other cody problem.
Note that the best scoring cody solution is unlikely to be the most efficient one.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2015년 5월 1일
편집: Stephen23 2015년 5월 1일
>> s_pt = [1,8,15];
>> e_pt = [3,12,20];
>> cell2mat(arrayfun(@(s,e)s:e, s_pt, e_pt, 'UniformOutput',false))
ans =
1 2 3 8 9 10 11 12 15 16 17 18 19 20
Although using a loop is likely to be faster...

댓글 수: 1

Thanks. This works. But, yeah, in this case, for loop is faster so I'll go with a loop.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 5월 1일

0 개 추천

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

Hi,
Thank you for your answer. This is not homework. I know the colon operator but this doesn't fit my requirement. If I tried:
s_pt:e_pt
This only gives me [1,2,3] rather than the answer I want.
Actually, my original question was trying to find out consecutive numbers (at least three consecutive numbers) in an array. For example, the array is [1,2,3,4,5,10,11,15,16,17]. The answer I expect would be [1,2,3,4,5,15,16,17]. The way I did was
1) find out the starting points of the consecutive numbers (more than 2 consecutive numbers). In this case, it would give me [1,15].
2) find out the length of each consecutive group. In this case, if would give me [5,3]. This can be used to calculate the ending points.
With these two arrays, back to the question I have, I need to find out evenly spaced array based on these two arrays.
On the contrary, the colon operator does fit your requirement - you just didn't use it the way you were supposed to . Try this:
out = [s_pt(1):e_pt(1), s_pt(2):e_pt(2), s_pt(3):e_pt(3)]
That will solve the exact situation you posted perfectly . If, on the other hand, you forgot to mention that this needs to be general enough to work for any two vectors that are the same length, then I invite you to read this and see if Stephen's code will do what you want. Then mark the best answer as "Accepted" and "Vote" for them.
Sorry, I did forget to mention that it should work for any two vectors.
If you want a simple, intuitive solution, just go through the arrays appending them onto an array, out, that you build up:
s_pt = [1,8,15];
e_pt = [3,12,20];
out = [];
for k = 1 : length(s_pt)
out = [out, s_pt(k):e_pt(k)];
end
% Show the final result in the command window.
out
Yes, this is what I have now. But I was thinking not to use for loop.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2015년 4월 30일

댓글:

2015년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by