- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

- 255 questions asked
- 2 answers
- 0 accepted answers
- reputation: 0

Asked by Prabha Kumaresan
on 10 Aug 2018

- 255 questions asked
- 2 answers
- 0 accepted answers
- reputation: 0

- 255 questions asked
- 2 answers
- 0 accepted answers
- reputation: 0

2 views (last 30 days)

2 views (last 30 days)

code:

A=1:12

while ~isempty(A)

B=ceil(sqrt(randi([numel(A)])))

C=A(randsample(length(A),B))

[~,idx]=find(ismember(A,C))

A(idx)=[]

end

The above code executes. but the outcome of B need to be increased subsequently,which means for first time B=1,next B=2,B=3 until B does not satisfy the subsequent condition.When b does not satisfy the subsequent condition,then B needs to start second time from beginning in such a way again B=1,B=2,B=3.In second time as A is 12,B can be 1 but B cannot be 2,so B needs to be treated again as 1.Similarly i need to run the code when i Change the value of A to 24,36 and so on.Could anyone help me to solve the issue.

- 6 questions asked
- 1,106 answers
- 528 accepted answers
- reputation: 2,996

Accepted Answer

I think I understand what you mean. If I do, then the code below should work for you.

A=1:12;

B=0;

while ~isempty(A)

B=B+1;

if B>numel(A),B=1;end

C=A(randsample(length(A),B));

[~,idx]=find(ismember(A,C));

A(idx)=[];

end

ok.Could you please help me how to take the values from A when B=1.

What this code does is increment B (the number of elements taken from A), until B becomes larger than A, which resets B to 1. So this code already takes values from A. I don't have the statistics toolbox anymore, so I can't test this exact code for you.

I notice I made a small mistake: B=1; before the loop should be B=0;

ok.thanks.It works.Could you please help me how to change the randsample in the command line C=A(randsample(length(A),B)) as C needs to take the value in the following manner for each iteration

iteration 1

1(first number from left)

iteration 2

2 12(second number from left,first number from right)

iteration 3

3 10 11(third number from left,third number from right,second number from right)

iteration 4

4 7 8 9(fourth number from left,sixth number from right,fifth number from right,four number from right)

iteration 5

5(fifth number from left)

iteration 6

6(sixth number from left)

Numbers needs to be taken from A.

Here you go, this code should do that.

clc

A=1:12;

B=0;

while ~isempty(A)

B=B+1;

if B>numel(A)

ind=1;

else

ind=[1 (numel(A)-B+2):numel(A) ];

end

C=A(ind);

A(ind)=[];

fprintf('C=[ ')

fprintf('%d ',C)

fprintf(']\n')

end

thanks.It works

for the above code for A=1:12,when ind=[1 (numel(A)-B+2):numel(A) ] condition is not getting satisfied it goes to ind=1 as there are only two numbers left it takes for each run one and executes.

but for A=1:24,when ind=[1 (numel(A)-B+2):numel(A) ] condition is not getting satisfied it goes to ind=1 as there three numbers left behind for first run it needs to take 1 number from left and for second run it needs to take 2 numbers one from left and one from right.Could you please help me on this.

So like this? (I put in 25 to show how it resets again)

clc

A=1:25;

B=0;

while ~isempty(A)

B=B+1;

if B>numel(A)

ind=1;

B=1;%reset B

else

ind=[1 (numel(A)-B+2):numel(A) ];

end

C=A(ind);

A(ind)=[];

fprintf('C=[ ')

fprintf('%d ',C)

fprintf(']\n')

end

thanks.It works

could you help me how to find all possibilities of A=1:12,in such a way the result should be

possibility 1

1

3 5

6 7 8

9 10 11 12

2

4

possibility 2

2

3 4

5 6 7

8 9 10 11

1

3

Possibility 3

3

4 5

6 7 8

9 10 11 12

1

2

and so on

And in all the possibilities first 1 number,followed by 2 number,then 3 number,4 number and again 1 number followed by 1 number.

Do you mean you want to repeat the code I gave you for [1:12], [2:12 1], [3:12 1:2] etc? Then the code below does that using circshift.

clc

A_master=1:12;

for k=1:numel(A_master)

fprintf('\nPossibility %02d:\n',k)

A=circshift(A_master,1-k);

B=0;

while ~isempty(A)

B=B+1;

if B>numel(A)

ind=1;

B=1;%reset B

else

ind=[1 (numel(A)-B+2):numel(A) ];

end

C=A(ind);

A(ind)=[];

fprintf('C=[ ')

fprintf('%d ',C)

fprintf(']\n')

end

end

But all the possibilities gives me the same number as given below.

Possibility 01:

C=[ 1 ]

C=[ 2 12 ]

C=[ 3 10 11]

C=[ 4 7 8 9]

C=[ 5 ]

C=[ 6 ]

Possibility 02:

C=[ 1 ]

C=[ 2 12 ]

C=[ 3 10 11]

C=[ 4 7 8 9]

C=[ 5 ]

C=[ 6 ]

Possibility 03:

C=[ 1 ]

C=[ 2 12 ]

C=[ 3 10 11]

C=[ 4 7 8 9]

C=[ 5 ]

C=[ 6 ]

and so on. could you please help me such that the possibilities should contain different numbers.

You must have edited my code. What code were you using to produce this result? (I also edited your comment to make your output much more readable in the forum)

Also, can you give a better description of what you mean by 'every possibility'? It is a bit difficult to generate the A vector when an adequate description is missing.

It works when i change the command line A=circshift(A_master,1-k) to A=circshift(A_master,1-k,2).

Could you please help me how is it possible to get 2 numbers when A=1:12 for each run in such a way first number with last number,second number with second last number and so on example the output should be

1 12

2 11

3 10

4 9

5 8

6 7

i got the above result.Could you please help me to get the result such that for each run three numbers needs to be displayed,in a manner

1 5 12

2 6 11

3 7 10

4 8 9

and for each run four numbers needs to be displayed in the following manner

1 4 7 12

2 5 8 11

3 6 9 10

code:

A=1:12;

while ~isempty(A)

B=2;

idx=[1 (numel(A)-B+2):numel(A) ]

C=A(idx;

A(idx)=[];

end

The above code gives the result by 2 numbers for each run.

But i need to have the result by 3 numbers and 4 numbers for each run in the following manner

for 3 numbers

1 5 12

2 6 11

3 7 10

4 8 9

for 4 numbers

1 4 7 12

2 5 8 11

3 6 9 10

Could you please help me on this.

So essentially you want to reshape your vector to some specified number of columns, and flip the even rows. The first you can do with the reshape function, the second step you can do with indexing.

Give it a try and put your code here if you are unsuccessful.

I tried with the code,but I am unable to get the result in the following manner

1 5 12

2 6 11

3 7 10

4 8 9

A=1:12

while ~isempty(A)

B=3

idx=[1 (numel(A)-B-4) (numel(A))]

C=A(idx)

A(idx)=[]

end

could you please help me on this.

If i use the following command

A=1:12

reshape(A,[4,3])

I am getting the following result

ans =

1 5 9

2 6 10

3 7 11

4 8 12

But i need to have the third column as

12

11

10

9

When i change the value of A=1:24,1:36,the reshape command needs to be changed.But i need to have the same reshape command even when i change the value of A.could you please help me on this.

I suspect this is what you need.

A=1:12;

C_matrix=reshape(A,[],3);

C_matrix(:,end)=C_matrix(end:-1:1,end);

s.but how to get the result with respect to indexing

You mean you want to have those values in a loop, just like with the while-loop?

A=1:12;

C_matrix=reshape(A,[],3);

C_matrix(:,end)=C_matrix(end:-1:1,end);

for k=1:size(C_matrix,1)

C=C_matrix(k,:)

end

ok.thanks.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply TodayUnable to complete the action because of changes made to the page. Reload the page to see its updated state.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

## 2 Comments

## Rik (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/414330-could-anyone-help-me-to-solve-the-issue-for-the-following-code#comment_598356

## Prabha Kumaresan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/414330-could-anyone-help-me-to-solve-the-issue-for-the-following-code#comment_598360

Sign in to comment.