yudha25
yudha25

Reputation: 51

combine two value of array in Matlab

Example, i have an array :

a = [2 3 1 2 4 5 6 4];
b = sort(a);
b = [1 2 2 3 4 4 5 6];

Now i want to combine two value of a and b :

c = [21 32 12 23 44 54 65 46]

then i do the sort of c :

d = [12 21 23 32 44 46 54 65]

and i combine again from c and d (first of c, same value of second c and first d, last d) :

e = [212 321 123 232 444 546 654 465]

then i do the sort of e :

f = [123 212 232 321 444 465 546 654]

and i combine again from e and f :

g = [2123 3212 1232 2321 4444 5465 6546 4654]

so on till the length of a that equals to 8.

Please help me.

Upvotes: 1

Views: 76

Answers (1)

David
David

Reputation: 8459

Try this:

a = [2 3 1 2 4 5 6 4]
for m=2:8
    b = sort(a)
    t = round(b-10*floor(b/10))
    a = 10*a+t
end

It looks to me like the algorithm just adds the last digit of each sorted list onto the corresponding number in the unsorted list. t is just the last digit in b, then 10*a+t shifts the existing digits in a and puts t at the end. Apologies if I have misunderstood the objective and this is the wrong algorithm, but it works with you example. I guess you will need to convince yourself whether the code does follow your rules.

Upvotes: 3

Related Questions