How to randomly swap 2 elements in an array?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Suppose that I have the below array:
C= [1 6 5 10 1 2 11 8 2 7 3 7 9 9]
How can I swap randomly only the elements inside the bold numbers
Assume after swap 11 and 3 : C= [1 6 5 10 1 2 3 8 2 7 11 7 9 9]
or swap 10 and " " between 9 9: C= [1 6 5 1 2 11 8 2 7 3 7 9 10 9]
採用された回答
I'm going to assume at least one of the swap elements must be an number, and that the swapped numbers are allowed to have the same value (so you can some one 9 with another 9).
C= [1 6 5 10 1 2 11 8 2 7 3 7 9 9];
bold=[1 5 6 9 10 12 13 14];
C_temp=cell(1,2*numel(C));
C_temp(2:2:end)=num2cell(C);
valid_idx=1:numel(C_temp);
valid_idx(bold*2)=[];
swap_idx=[1 1];%invalid output to enter the loop
while ~any(mod(swap_idx,2)==0) || ... %at least 1 needs to be a number
swap_idx(1)==swap_idx(2) %the indices must be different
swap_idx=valid_idx(randperm(numel(valid_idx),2));
end
%perform swap
C_temp(swap_idx)=C_temp(swap_idx(end:-1:1));
D=cell2mat(C_temp);
clc,disp(C),disp(D)
12 件のコメント
Thank you! but can you make it more flexible? I got C from ramdomly displayed.
So these bold numbers are not in the fit position.
And your code display this when I run:
1 6 5 10 1 2 11 8 2 7 3 7 9 9
6 1 5 10 1 2 11 8 2 7 3 7 9 9
6 is out of 1 1
Rik
2019 年 5 月 1 日
You didn't describe how you get the bold numbers, so you'll have to figure that out from your input. I might be able to help you there, but I can't read your mind.
Your output should not be possible, that is what the while loop is for. What do the two conditions return if you try them on their own?
Hang Vu
2019 年 5 月 1 日
it showed this when I run:
C =
1 6 5 10 1 2 11 8 2 7 3 7 9 9
swap_idx =
23 15
swap_idx =
21 3
swap_idx =
11 25
swap_idx =
1 4
6 1 5 10 1 2 11 8 2 7 3 7 9 9
this is how I got C:
I have D = [170, 80, -30, 0, -50, -30, 20, -60, 100, -60 -20];
A contains all + elements and I repelem them( those bold number)
B contains all - elements
Then I randomly put B into A to create C.
Now I want to swap elements between bold numbers
Rik
2019 年 5 月 1 日
I don't really understand what you mean in your description. At any rate, you should be able to write some code that returns a vector with which elements are bold.
About the result you had the first point: before the first number element there is an empty cell where swapping is allowed. If you don't want that, you can put in an extra condition in the while statement:
while ~any(mod(swap_idx,2)==0) || ... %at least 1 needs to be a number
swap_idx(1)==swap_idx(2) || ... %the indices must be different
any(swap_idx==1) %the first element (1 before the first number) isn't allowed
swap_idx=valid_idx(randperm(numel(valid_idx),2));
end
Hang Vu
2019 年 5 月 1 日
yes, thank you so much for yor time!
I am sorry for bothering you, but I wonder why sometimes the result is still the same after swapping:
C =
1 6 5 10 1 2 11 8 2 7 3 7 9 9
swap_idx =
23 9
swap_idx =
4 3
1 6 5 10 1 2 11 8 2 7 3 7 9 9
Rik
2019 年 5 月 4 日
Look at the indices being swapped. What do you think is happening?
Hang Vu
2019 年 5 月 4 日
It was invalid then it swaps again, but most of the time it returns the same array.
Rik
2019 年 5 月 4 日
Look at the indices. What is being swapped if they are 4 and 3?
I got it, it's nothing at 3 and number 6 at 4. Thank you! I will try to fix it!
Rik
2019 年 5 月 4 日
This should prevent the two swapped values to be adjacent (which swaps a value with a gap, meaning it seems the array stays the same).
while ~any(mod(swap_idx,2)==0) || ... %at least 1 needs to be a number
any(swap_idx==1) || ... %the first element (1 before the first number) isn't allowed
abs(diff(swap_idx))<=1 %the indices must be different and more than 1 appart
swap_idx=valid_idx(randperm(numel(valid_idx),2));
end
Hang Vu
2019 年 5 月 8 日
Thank you for the help!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
