Could anyone help me to solve the issue for the following code
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2018-8-10
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
2018-8-10
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.
采纳的回答
Rik
2018-8-10
编辑:Rik
2018-8-10
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 个评论
Rik
2018-8-10
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
2018-8-10
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
2018-8-10
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
2018-8-13
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
2018-8-13
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
2018-8-13
编辑:Rik
2018-8-13
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
2018-8-13
编辑:Rik
2018-8-13
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
2018-8-13
编辑:Rik
2018-8-13
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
2018-8-13
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
2018-8-14
It works when i change the command line A=circshift(A_master,1-k) to A=circshift(A_master,1-k,2).
Prabha Kumaresan
2018-8-14
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
2018-8-14
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
2018-8-14
What code have you tried yourself? This shouldn't be too difficult to figure out, given the code in this thread.
Prabha Kumaresan
2018-8-14
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
2018-8-14
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
2018-8-14
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
2018-8-14
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
2018-8-14
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
2018-8-14
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
2018-8-14
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)