code that randomly changes columns in matrix throws error

I have a matrix
This matrix consists of four rows. Some numbers are integers and some are decimals. I want to print a code on this matrix, but it gives an error like this:
Error using randperm
Size inputs must be integers.
Error in deneme (line 49)
new_idx = randperm(size(D,2)/2)*2-[1; 0];
>>
My code is :
new_idx = randperm(size(D,2)/2)*2-[1; 0];
rassalnew= D(:,new_idx(:))
What can I do to fix this problem?
Thank for help
D matrix:
1 2 3 4
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000
510.090000000000 26.4600000000000 22.3400000000000 169.680000000000
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000

 採用された回答

Voss
Voss 2022 年 3 月 23 日
編集済み: Voss 2022 年 3 月 23 日

0 投票

It seems to be working for that matrix D:
D = [ ...
1 2 3 4
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000
510.090000000000 26.4600000000000 22.3400000000000 169.680000000000
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000];
new_idx = randperm(size(D,2)/2)*2-[1; 0];
rassalnew= D(:,new_idx(:))
rassalnew = 4×4
3.0000 4.0000 1.0000 2.0000 22.3400 188.3000 510.0900 26.4600 22.3400 169.6800 510.0900 26.4600 22.3400 188.3000 510.0900 26.4600
But you will get that error if D has an odd number of columns:
D = [ ...
0 1 2 3 4
300 510.090000000000 26.4600000000000 22.3400000000000 188.300000000000
400 510.090000000000 26.4600000000000 22.3400000000000 169.680000000000
200 510.090000000000 26.4600000000000 22.3400000000000 188.300000000000];
new_idx = randperm(size(D,2)/2)*2-[1; 0];
Error using randperm
Size inputs must be integers.
rassalnew= D(:,new_idx(:))

7 件のコメント

Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 23 日
This is a big matrix in original. so I can't manually enter the first line. How can I convert it to 1.000?
Voss
Voss 2022 年 3 月 23 日
Sorry, I don't understand what you mean by, "manually enter the first line" or "convert it to 1.000". Can you please explain?
I should emphasize that the error happens whenever D has an odd number of columns, and the error has nothing to do with the contents of D (e.g., it does not matter whether the elements are integers or not). The assumption in using randperm() to rearrange the columns in pairs is that there are an even number of columns.
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 23 日
I understand what you mean now. Thank you for your help
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 23 日
If I wanted to randomly change the columns one by one, how should I do it? could i use randperm?
Voss
Voss 2022 年 3 月 23 日
編集済み: Voss 2022 年 3 月 23 日
You're welcome!
Yes, you can use randperm() for that:
D = [ ...
1 2 3 4
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000
510.090000000000 26.4600000000000 22.3400000000000 169.680000000000
510.090000000000 26.4600000000000 22.3400000000000 188.300000000000];
new_idx = randperm(size(D,2));
rassalnew= D(:,new_idx(:))
rassalnew = 4×4
3.0000 2.0000 1.0000 4.0000 22.3400 26.4600 510.0900 188.3000 22.3400 26.4600 510.0900 169.6800 22.3400 26.4600 510.0900 188.3000
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 23 日
Thanks a lot of :)
Voss
Voss 2022 年 3 月 23 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

タグ

質問済み:

2022 年 3 月 23 日

コメント済み:

2022 年 3 月 23 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by