이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Could anyone help me to solve the issue for the following code
조회 수: 2 (최근 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에서 Linear Algebra에 대해 자세히 알아보기
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 (한국어)