You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Could anyone help me to solve the issue for the following code
1 view (last 30 days)
Show older comments
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.
2 Comments
Rik
on 10 Aug 2018
I have no idea what you mean in your description, so please explain it a bit more step by step. It sounds like you want to have B increment on each iteration. If you want that, then you shouldn't be using random generation and you should remove elements in A.
Prabha Kumaresan
on 10 Aug 2018
yes. you are correct.B needs to get incremented on each iteration. When A=12,till 4 iterations B can take corresponding values from A on each increment.But for 5th iteration it is not possible to take 5 values from A as the remaining number of values in A are 2. So once again the iteration needs to start from the beginning.As a result for 5th iteration B needs to take one value from A.For 6th iteration B needs to take 2 values from A,as there are only one value is left,so for 6th iteration B once again needs to take the last value from A.
Yes i should not use random generation.Could you please help me how to overcome it.
Accepted Answer
Rik
on 10 Aug 2018
Edited: Rik
on 10 Aug 2018
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
26 Comments
Rik
on 10 Aug 2018
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;
Prabha Kumaresan
on 10 Aug 2018
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)
Rik
on 10 Aug 2018
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
Prabha Kumaresan
on 13 Aug 2018
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.
Rik
on 13 Aug 2018
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
Prabha Kumaresan
on 13 Aug 2018
Edited: Rik
on 13 Aug 2018
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.
Rik
on 13 Aug 2018
Edited: Rik
on 13 Aug 2018
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
Prabha Kumaresan
on 13 Aug 2018
Edited: Rik
on 13 Aug 2018
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.
Rik
on 13 Aug 2018
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.
Prabha Kumaresan
on 14 Aug 2018
It works when i change the command line A=circshift(A_master,1-k) to A=circshift(A_master,1-k,2).
Prabha Kumaresan
on 14 Aug 2018
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
Prabha Kumaresan
on 14 Aug 2018
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
Rik
on 14 Aug 2018
What code have you tried yourself? This shouldn't be too difficult to figure out, given the code in this thread.
Prabha Kumaresan
on 14 Aug 2018
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.
Rik
on 14 Aug 2018
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.
Prabha Kumaresan
on 14 Aug 2018
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.
Rik
on 14 Aug 2018
Try using the reshape function to change the shape of your A vector to a matrix with the correct number of columns. The while loop can be used later on (although we can replace it by a for-loop now).
Prabha Kumaresan
on 14 Aug 2018
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.
Rik
on 14 Aug 2018
I suspect this is what you need.
A=1:12;
C_matrix=reshape(A,[],3);
C_matrix(:,end)=C_matrix(end:-1:1,end);
Rik
on 14 Aug 2018
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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
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: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)