Mathematical operation on arrays

조회 수: 4 (최근 30일)
Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia 2021년 8월 18일
댓글: the cyclist 2021년 8월 19일
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
Wan Ji
Wan Ji 2021년 8월 18일
You need tell how to delete value in b,c and d by using value from vector a
Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia 2021년 8월 18일
The values that are less than the value chosen from the first vector

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2021년 8월 18일
% 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)
ans = 1×2
7 1
size(b)
ans = 1×2
4 1
size(c)
ans = 1×2
5 1
size(d)
ans = 1×2
5 1
  댓글 수: 4
Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia 2021년 8월 19일
I meant if instead of a_random ...I would like to refer for a specific value in the vector a ( the value in the 2nd row for example)
the cyclist
the cyclist 2021년 8월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by