How do I combine 2 arrays into one but in a specific order.

20 次查看(过去 30 天)
I need to combine 2 arrays: one contains even values and one containes odd values. How do I go about doing this? I attempted to do a while loop and place the values in but i cant seem to set it up right. Any help would be great :)
temp_even = [9.2 9.0 6.8 5.9 7.2 9.2 10.9 10.6 10.3 8.2 8.8 7.5];
temp_odd = [9.6 7.7 6.0 4.3 8.9 10.2 10.6 10.7 8.6 8.0 7.1 7.5];
  2 个评论
Jaya
Jaya 2021-7-7
编辑:Jaya 2021-7-7
Do you mean just combining them in a simple manner? Then you can use
res= horzcat(temp_even,temp_odd)
and then just use sort using
sort(res,'ascend')
%or this sort(res, 'descend')
Or is it something else you want to do? If you want to place one even number one odd number as you traverse the array then you may have to use loop, check each element and place one by one in a third array. This is what I would do but I don't know if there is any smart way to do it.
Sergey Popov
Sergey Popov 2021-7-7
I need them in that specific order alternating between the even and odd values I tried using a loop but it didnt work, could you give an example?

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2021-7-7
编辑:Matt J 2021-7-7
temp_odd = [9.6 7.7 6.0 4.3 8.9 10.2 10.6 10.7 8.6 8.0 7.1 7.5];
temp_even = [9.2 9.0 6.8 5.9 7.2 9.2 10.9 10.6 10.3 8.2 8.8 7.5];
result=reshape( [temp_odd;temp_even], 1,[])
result = 1×24
9.6000 9.2000 7.7000 9.0000 6.0000 6.8000 4.3000 5.9000 8.9000 7.2000 10.2000 9.2000 10.6000 10.9000 10.7000 10.6000 8.6000 10.3000 8.0000 8.2000 7.1000 8.8000 7.5000 7.5000

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by