code that randomly changes columns in matrix throws error

조회 수: 1 (최근 30일)
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 23일
댓글: Voss 2022년 3월 23일
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일
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일
Thanks a lot of :)
Voss
Voss 2022년 3월 23일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by