make a for cycle that will choose 1 number from vector lhodnoty 3 times in matlab

You can use a for loop to randomly choose a number from a vector and repeat it three times. Here's an example code:

main.m
lhodnoty = [1 2 3 4 5]; % example vector of values
chosen = zeros(1,3); % preallocate array for chosen values
for i = 1:3
    idx = randi(length(lhodnoty)); % generate random index
    chosen(i) = lhodnoty(idx); % select value at index and store it
end
disp(chosen) % display the chosen values
294 chars
8 lines

This will output an array chosen with 3 randomly selected values from the lhodnoty vector.

Note that randi(n) generates a random integer between 1 and n, inclusive. So randi(length(lhodnoty)) gives a random index for the lhodnoty vector. The selected value is then stored in the chosen array, which is preallocated with zeros(1,3) to store three values.

You can adjust the size and content of the lhodnoty vector depending on your needs.

related categories

gistlibby LogSnag