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

23 Ansichten (letzte 30 Tage)
Sergey Popov
Sergey Popov am 7 Jul. 2021
Bearbeitet: Matt J am 7 Jul. 2021
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 Kommentare
Jaya
Jaya am 7 Jul. 2021
Bearbeitet: Jaya am 7 Jul. 2021
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 am 7 Jul. 2021
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 7 Jul. 2021
Bearbeitet: Matt J am 7 Jul. 2021
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

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by