Mathematical operation on arrays
Show older comments
Dear All, I hope that all are well. I have 4 column vectors, a b c and d. I would like to write a code that take a random value for the first vector a and using that value to delete each value less than it from the vectors b c and d... delete not transfer them to zeros.
I would highly appreciate your help.
2 Comments
Wan Ji
on 18 Aug 2021
You need tell how to delete value in b,c and d by using value from vector a
Mohammed Lamine Mekhalfia
on 18 Aug 2021
Answers (1)
% Set random number seed, for reproducible results in this example
rng default
% Make up the input data
N = 7;
a = rand(N,1);
b = rand(N,1);
c = rand(N,1);
d = rand(N,1);
% Pick random value from a
a_rand = a(randi(numel(a)));
% Delete values from other vectors if they are less than a_rand
b(b<a_rand) = [];
c(c<a_rand) = [];
d(d<a_rand) = [];
% Show the sizes of the resulting vectors
size(a)
size(b)
size(c)
size(d)
4 Comments
Mohammed Lamine Mekhalfia
on 19 Aug 2021
the cyclist
on 19 Aug 2021
I'm sorry, but I don't understand this question. Specifically,
- I don't know what you are referring to when you say "that value"
- Your original question referencing several vectors, so I am not sure which you mean here
If your original question has been answered, I suggest accepting the answer here, and opening a new question. Try to be as clear and complete as you can, and perhaps give an example. Feel free to tag me (by using @the cyclist) on the new question.
Mohammed Lamine Mekhalfia
on 19 Aug 2021
the cyclist
on 19 Aug 2021
Instead of
a_rand = a(randi(numel(a)));
use
a(2)
as the value.
This is a very basic MATLAB question. You might want to watch the MATLAB Onramp tutorial to learn the basics.
Categories
Find more on MATLAB 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!