Random Matrix creation from the elements of a given vector

Dear all,
I have a vector A containing the elements [EI, VI, I, SI, M, SS, S, VS, ES]. I want to create a 7x20 random matrix using the elements from vector A. How can I do this?

 Accepted Answer

M = A(randi(numel(A),7,20));

8 Comments

Thanks Voss for your help. I have one more query:
If I have two vectors, A = [EI, VI, I, SI, M, SS, S, VS, ES] and B = [CU, VU, SU, N, SR, VR, CR]. I want to create a 7x20 random matrix with elements in the form of ordered pairs, where the first element comes from vector A and the second element comes from vector B. How can I do this?
Like this?
A = 1:9;
B = 10:16;
MA = A(randi(numel(A),1,70));
MB = B(randi(numel(B),1,70));
M = reshape([MA; MB],[],7).'
M = 7x20
7 13 6 10 7 16 2 10 8 11 4 10 2 12 9 13 9 10 2 10 7 13 7 14 3 10 4 14 3 11 4 16 7 14 3 10 2 15 8 16 1 15 7 11 3 13 8 13 5 10 3 12 9 15 6 15 9 16 7 13 3 12 4 11 4 13 6 15 8 16 3 12 8 16 5 16 4 11 5 13 2 10 2 11 5 15 1 10 4 16 3 13 3 14 8 11 8 15 1 10 1 13 3 10 6 15 7 14 2 15 3 12 5 12 1 15 3 13 3 10 2 14 9 14 6 14 5 14 5 10 7 16 2 14 2 13 1 12 7 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You're welcome! Any questions, let me know.
If I have two intervals, A = [0,1] and B = [0,1]. I want to create a 5x10 random matrix with elements in the form of ordered pairs as (a,b), where the first element a comes from A and the second element b comes from B. Each element satisfy the condition a+b<=1. How can I do this in MATLAB?
I don't know what it means for a marix to have an ordered pair as an element, so I'm using two 5x10 matrices instead.
a = rand(5,10);
b = rand(5,10);
while true
idx = a+b > 1;
b(idx) = rand(nnz(idx),1);
if all(a+b <= 1,'all')
break
end
end
disp(a)
0.6803 0.1873 0.3608 0.1559 0.5003 0.0737 0.4319 0.9768 0.6602 0.7798 0.7782 0.5313 0.2842 0.2559 0.6236 0.2574 0.2294 0.1369 0.3090 0.7058 0.1423 0.9112 0.9561 0.1393 0.4185 0.1359 0.7159 0.1865 0.4885 0.3567 0.7925 0.7438 0.2276 0.8216 0.8435 0.8321 0.3346 0.5175 0.2961 0.4162 0.2621 0.3928 0.0201 0.8900 0.4407 0.2422 0.2344 0.4705 0.9059 0.6581
disp(b)
0.3008 0.1674 0.5079 0.4175 0.4797 0.6370 0.0269 0.0078 0.0142 0.0654 0.2051 0.2140 0.2644 0.0316 0.0441 0.6730 0.7432 0.0022 0.4761 0.1458 0.2677 0.0308 0.0294 0.5593 0.5261 0.4142 0.1733 0.7507 0.2443 0.0694 0.0950 0.1419 0.3965 0.1038 0.0673 0.0707 0.4720 0.1438 0.4402 0.4177 0.7122 0.2491 0.5326 0.0557 0.1949 0.0756 0.3784 0.2126 0.0554 0.2765
disp(a+b)
0.9811 0.3547 0.8687 0.5735 0.9800 0.7107 0.4587 0.9846 0.6744 0.8452 0.9833 0.7453 0.5486 0.2875 0.6677 0.9304 0.9726 0.1392 0.7852 0.8516 0.4100 0.9420 0.9855 0.6987 0.9446 0.5501 0.8892 0.9372 0.7329 0.4261 0.8875 0.8857 0.6241 0.9254 0.9108 0.9028 0.8066 0.6612 0.7363 0.8340 0.9743 0.6419 0.5526 0.9457 0.6357 0.3178 0.6128 0.6831 0.9613 0.9345

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 21 May 2024

Commented:

on 23 May 2024

Community Treasure Hunt

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

Start Hunting!