Loop to distribute the K in Two Variables

The Value of K = 1:10; Must be distributed as follows:
When K=1 is an odd number then k1=1 and k2=0.
When K=2 even number then We will split it equally k1=1 and k2=1.
When K=3 is an odd number, We will see the sum of all values in k1 and k2, and then the smallest sum which in this case k2 will take the bigger number (2) While k1 will take (1).
When K=4 even number then We will split it equally k1=2 and k2=2.
When K=5 is an odd number, We will see the sum of all values in k1 and k2, and then the smallest sum, and in this case, k1 and k2 are equal, so you can give the bigger value to anyone of them.
We will continue like this and store the values of k1 and k2
k1=[1 1 1 2 .....]
k2=[0 1 2 2 .....]

 Accepted Answer

Ali, have you tried this:
K = 1 : 10;
k1 = floor(K / 2)
k1 = 1×10
0 1 1 2 2 3 3 4 4 5
k2 = K - k1
k2 = 1×10
1 1 2 2 3 3 4 4 5 5

More Answers (1)

I am slightly confused about a couple of aspects, but something like this should work
clear,clc
K = 1:10;
k0 = zeros(2,length(K));
k0(1,1) = 1;
for i = 2:length(K)
if mod(i,2) == 0
k0(:,i) = [i/2;i/2];
else
s = sum(k0,2);
k0(:,i) = flip(s);
end
end
k1 = k0(1,:)
k2 = k0(2,:)
For K = 1:10 get
k1 = [1 1 1 2 5 3 13 4 30 5]
k2 = [0 1 2 2 5 3 13 4 30 5]

7 Comments

The Answer is Wrong, K=5 Means you will have k1=2 and k2=3 or k1=3 and k2=2, and Then When K=6 Means k1=3 and k2=3 and so on.
That's why I said some aspects where unclear.
From what you're saying, it seems that when the value is odd, you need to split it in 2 integers that differ of only 1 and then assign the largest integer to the array which elements summation up to that point is smallest. This means that, for instance, when K = 7 you'll have k1 = 4 and k2 = 3?
If this is the case, then this will work
clear,clc
K = 1:10;
k0 = zeros(2,length(K));
k0(1,1) = 1;
for i = 2:length(K)
if mod(i,2) == 0
k0(:,i) = [i/2;i/2];
else
s = sum(k0,2);
n = [i/2-0.5;i/2+0.5];
if s(1) >= s(2)
k0(:,i) = n;
else
k0(:,i) = flip(n);
end
end
end
k1 = k0(1,:)
k2 = k0(2,:)
The new result is
k1 = [1 1 1 2 2 3 4 4 4 5]
k2 = [0 1 2 2 3 3 3 4 5 5]
If the code works fine, please accept the answer.
@Ali Algushti, then fix it if it's wrong. You're not allowed to turn in Davide's answer as your own original work anyway, right? That's not allowed at your university is it?
@Image Analyst First I am not Turning Anything, and Second, you have to be polite your Attitude is not Acceptable
I assumed that my solution was correct from the moment that @Ali Algushti accepted the answer. So I am slightly confused by the follow up comment of @Image Analyst. Please do let me know if I have infringed some policy of the Matlab answer forum. Albeit I have been using Matlab for a while now, I have only recently started answering questions.
I would appreciate it if you would direct me to the documentation about which Attitudes are "Acceptable" while answering questions here.

Sign in to comment.

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!