Increassing an array by a random value

조회 수: 1 (최근 30일)
Róbert Straka
Róbert Straka 2021년 2월 16일
답변: Walter Roberson 2021년 2월 16일
Hello guys,
lets say I have 3 arrays and 3 random numbers that I use to increase elements in the 3 arrays. Something like this:
a = rand()*(2)-1;
b = rand()*(2)-1;
c = rand()*(2)-1;
X = [1...10];
Y = [11...20];
Z = [21...30];
Now the easiest way to increase the elements in arrays by those number would be:
X1 = X+a
Y1 = Y+b
Z1 = Z+c
My question is if there is a way to add those random numbers randomly, so it will alway choose a random number from a,b,c to every array

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 16일
a = rand()*(2)-1;
b = rand()*(2)-1;
c = rand()*(2)-1;
abc = [a,b,c]
abc = 1×3
-0.2610 0.8958 0.0643
X = [1:10];
Y = [11:20];
Z = [21:30];
XYZ = [X;Y;Z];
ridx = randi(3, size(XYZ))
ridx = 3×10
3 3 3 3 2 1 2 3 1 1 1 3 2 3 1 3 1 3 2 2 3 3 2 3 2 1 1 2 1 3
XYZ = XYZ + abc(ridx);
X1 = XYZ(1,:)
X1 = 1×10
1.0643 2.0643 3.0643 4.0643 5.8958 5.7390 7.8958 8.0643 8.7390 9.7390
Y1 = XYZ(2,:)
Y1 = 1×10
10.7390 12.0643 13.8958 14.0643 14.7390 16.0643 16.7390 18.0643 19.8958 20.8958
Z1 = XYZ(3,:)
Z1 = 1×10
21.0643 22.0643 23.8958 24.0643 25.8958 25.7390 26.7390 28.8958 28.7390 30.0643

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by