count of negative vector elements

31 次查看(过去 30 天)
Firuze
Firuze 2024-1-15
Hello everyone,
Counting the negative elements of a 10-element vector and assigning the result to variable b, counting the positive or zeros and assigning them to variable c, how to do it? I am new to Matlab, I would be very happy if you could solve it.
Does it make sense to use sign x?
  2 个评论
Walter Roberson
Walter Roberson 2024-1-15
Approximate translation:
Hello everyone,How to count the negative elements of a 10-element vector and assign the result to variable b, count the positive or zeros and assign them to variable c?Does it make sense to use sign x?I am new to Matlab, I would be very happy if you could solve it.
John D'Errico
John D'Errico 2024-1-15
编辑:John D'Errico 2024-1-15
Can you use sign(x)? It gets you close. But even then, will it tell you exactly what you need to know?
Will the sign function tell you if the number is less than zero? What operator will tell you that information DIRECTLY? Hint:
help le
<= Less than or equal. A <= B does element by element comparisons between A and B and returns an array with elements set to logical 1 (TRUE) where the relation is true and elements set to logical 0 (FALSE) where it is not. A and B must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are either the same or one of them is 1. C = LE(A,B) is called for the syntax 'A <= B' when A or B is an object. See MATLAB Operators and Special Characters for more details. Documentation for le doc le Other uses of le categorical/le duration/le mtree/le string/le codistributed/le embedded.fi/le qrandstream/le symbolic/le datetime/le gpuArray/le RandStream/le tabular/le dlarray/le handle/le
As you can see, the less than or equal to operator, used and written in MATLAB as <= will tell you that information directly.

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2024-1-15
Using sign() could be okay. The rough outline would be to take the sign() of the values, then to test the sign() twice, once for -1 and once for +1 .
But it is less work to test twice, once for < 0 and once for > 0

Hassaan
Hassaan 2024-1-15
编辑:Hassaan 2024-1-15
@Firuze Few pointers(hints) to help you get started and ease your MATLAB journey.
  1. Logical Indexing: Use logical conditions to create a "mask" that identifies which elements of your vector meet certain criteria (e.g., being negative).
  2. The sum Function: This function, when used on a logical array (an array of true and false values), will count the number of true values.
  3. Relational Operators: Use < to check for negative elements and >= to check for positive elements or zeros.
  4. Vectors: Remember that a vector in MATLAB is a one-dimensional array, so you'll be performing operations on each element.
Z = [your_vector_elements]; % Assume Z is your vector. Hope you know how to do that.
% See my above hints.
%% your logic [ different ways to achieve ]
% Initialize variables to store counts
b = 0; % Will hold the count of negative elements
c = 0; % Will hold the count of positive elements or zeros
%
%
%
%
%
% Display the results
fprintf('Number of negative elements: %d\n', b);
fprintf('Number of positive elements or zeros: %d\n', c);
Reference
Free hands-on course [Official]
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 个评论
Walter Roberson
Walter Roberson 2024-1-15
sum() is not needed here -- after you have separated the elements into two separate vectors, check the length of the vectors to count them.
Hassaan
Hassaan 2024-1-15
编辑:Hassaan 2024-1-15
Yes I agree one of many ways to do so.

请先登录,再进行评论。


Jona Grümbel
Jona Grümbel 2024-1-19
I think the easiest way is to use logical indexing. It works like this:
A = -4:1:5; % this is your 10 element vector as an example
B = A(A < 0); % A<0 returns a logical vector with 1 for elements for which A(i)<0 and zero for all others
NumNeg = numel(B);
C = A(A == 0);
NumZero = numel(C);

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by