i want to generate random matrix "P" but every alternate entry in each of the column is repeating

6 views (last 30 days)
for i = 1:22
for j=1:22
if mod(j,2)==0
P(j,i) = y_random(i); %y coordinate
else
P(j,i) = x_random(i); %x coordinate
end
end
end
  3 Comments
Torsten
Torsten on 4 Oct 2022
Edited: Torsten on 4 Oct 2022
And why do you define two vectors ? How should x_random and y_random be used to define P(i,j) ?
Maybe something like this ?
x_random = rand(22,1);
y_random = rand(22,1);
P = zeros(22);
for i = 1:22
for j=1:22
if mod(j,2)==0
P(j,i) = y_random(randi(22)); %y coordinate
else
P(j,i) = x_random(randi(22)); %x coordinate
end
end
end
P
P = 22×22
0.4638 0.0297 0.9974 0.3303 0.5157 0.3903 0.9375 0.4202 0.3303 0.6418 0.7993 0.6285 0.7993 0.3889 0.0167 0.9974 0.6862 0.3860 0.7993 0.0913 0.3903 0.1146 0.9660 0.0085 0.9169 0.8784 0.8784 0.2872 0.6207 0.9660 0.2997 0.5111 0.2997 0.2997 0.1494 0.2997 0.9169 0.9469 0.2997 0.5111 0.3422 0.2872 0.6130 0.2872 0.6862 0.1146 0.7993 0.0913 0.0913 0.3889 0.0913 0.6862 0.5157 0.5157 0.3303 0.1516 0.3303 0.0913 0.9375 0.6862 0.0167 0.0167 0.6285 0.9542 0.1516 0.6862 0.2872 0.9469 0.4641 0.2997 0.5111 0.9469 0.2040 0.6130 0.9469 0.2997 0.8784 0.8784 0.9846 0.3422 0.9169 0.1494 0.1494 0.8354 0.7551 0.4641 0.9846 0.2040 0.3303 0.3903 0.4202 0.1146 0.4638 0.6862 0.0913 0.5157 0.4638 0.4202 0.1516 0.3860 0.3854 0.3903 0.3303 0.3303 0.0913 0.1516 0.0913 0.3854 0.4638 0.3860 0.8252 0.9883 0.9660 0.7551 0.6851 0.3422 0.2078 0.9883 0.2872 0.2040 0.9469 0.6130 0.2040 0.9660 0.8784 0.2872 0.8252 0.3422 0.0085 0.4641 0.6851 0.9883 0.6418 0.3303 0.6862 0.9974 0.3303 0.4202 0.4638 0.5157 0.5157 0.6862 0.4638 0.0167 0.8718 0.4638 0.9974 0.3860 0.7993 0.6862 0.9542 0.9974 0.4638 0.7805 0.6207 0.8050 0.8050 0.8050 0.9469 0.6130 0.8784 0.3422 0.2040 0.8784 0.9846 0.9469 0.6130 0.6851 0.2040 0.8354 0.8050 0.2078 0.8354 0.9846 0.8354 0.3422 0.9542 0.4202 0.7805 0.0297 0.3903 0.0167 0.9375 0.9375 0.1516 0.7993 0.1146 0.3303 0.3860 0.6285 0.9542 0.9974 0.3303 0.3903 0.7993 0.7993 0.7805 0.6285 0.0085 0.9883 0.2997 0.8354 0.3422 0.6207 0.7551 0.9846 0.4641 0.9169 0.1494 0.6130 0.9846 0.6207 0.6207 0.2997 0.9883 0.0085 0.9169 0.9883 0.8784 0.6130
or
x_random = rand(22,1);
y_random = rand(22,1);
P = zeros(22);
for j = 1:22
if mod(j,2)==0
P(j,:) = y_random(randperm(22));
else
P(j,:) = x_random(randperm(22));
end
end
P
P = 22×22
0.7590 0.2697 0.2642 0.6825 0.6345 0.4673 0.6329 0.9049 0.1416 0.5564 0.7226 0.1874 0.4541 0.2895 0.2086 0.5237 0.8834 0.8906 0.3734 0.9382 0.8326 0.4598 0.8514 0.9793 0.5214 0.7886 0.6236 0.6631 0.5441 0.5257 0.5851 0.1617 0.8995 0.0537 0.4587 0.2627 0.1772 0.4890 0.6674 0.8498 0.4195 0.3676 0.5692 0.9723 0.4598 0.5237 0.1874 0.6825 0.6345 0.2642 0.6329 0.4673 0.2086 0.9382 0.7590 0.8834 0.9049 0.1416 0.4541 0.8326 0.5564 0.8906 0.3734 0.2895 0.2697 0.7226 0.8995 0.4195 0.0537 0.7886 0.9793 0.9723 0.5692 0.1772 0.8498 0.6674 0.5257 0.2627 0.6236 0.1617 0.5851 0.5441 0.3676 0.8514 0.4890 0.4587 0.6631 0.5214 0.2697 0.1416 0.6329 0.2642 0.3734 0.4598 0.1874 0.8834 0.4673 0.6345 0.7226 0.6825 0.5564 0.4541 0.7590 0.9049 0.5237 0.2895 0.8326 0.8906 0.9382 0.2086 0.6236 0.7886 0.5214 0.1772 0.6631 0.8514 0.8498 0.9723 0.4195 0.4587 0.9793 0.5257 0.5692 0.1617 0.6674 0.8995 0.4890 0.2627 0.5441 0.0537 0.5851 0.3676 0.2895 0.1416 0.6329 0.9049 0.8906 0.5564 0.9382 0.7226 0.4541 0.8834 0.7590 0.2697 0.1874 0.2086 0.8326 0.2642 0.3734 0.4598 0.6345 0.6825 0.4673 0.5237 0.8514 0.4195 0.9723 0.7886 0.6236 0.1772 0.5441 0.8498 0.5214 0.3676 0.5851 0.4890 0.4587 0.2627 0.5257 0.6631 0.8995 0.5692 0.1617 0.6674 0.0537 0.9793 0.1874 0.2642 0.2086 0.4541 0.3734 0.9382 0.8834 0.6825 0.6329 0.5564 0.5237 0.2895 0.1416 0.4598 0.9049 0.4673 0.7226 0.7590 0.6345 0.8326 0.8906 0.2697 0.9723 0.6674 0.5441 0.5692 0.2627 0.1772 0.8514 0.0537 0.3676 0.9793 0.6631 0.5214 0.5851 0.7886 0.4587 0.6236 0.5257 0.4195 0.4890 0.1617 0.8995 0.8498

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!