Writing a vector with randi and extract the elements in another variable
2 views (last 30 days)
Show older comments
Dacian Andrei Firca
on 3 Mar 2022
Commented: Dacian Andrei Firca
on 3 Mar 2022
Hi guys. How can i randomly generate a vector with 100 elements that contains integers between 40 and 120 than save in a variable P_vec the positions where the elements have values between 50 and 70?
Thank you!
0 Comments
Accepted Answer
DGM
on 3 Mar 2022
Edited: DGM
on 3 Mar 2022
This will give you a logical mask describing the location of elements within the closed interval [50 70]. You can call it what you want.
A = randi([40 120],100,1);
idx_logical = (A>=50) & (A<=70);
While logical indexing is often the more efficient approach, sometimes you might want the actual indices. If you want the linear indices of those locations:
idx_linear = find(idx_logical);
More Answers (0)
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!