find the first three smallest values in a vector in matlab

One way to find the first three smallest values in a vector in MATLAB is by sorting the vector in ascending order and taking the first three elements using indexing. Here's an example code snippet:

main.m
vec = [6, 2, 8, 1, 3, 9, 4, 5, 7]; % example vector
sorted_vec = sort(vec); % sort vector in ascending order
first_three = sorted_vec(1:3); % take first three elements
disp(first_three); % display the result
208 chars
5 lines

Output:

main.m
1     2     3
14 chars
2 lines

gistlibby LogSnag