count of negative vector elements

60 Ansichten (letzte 30 Tage)
Firuze
Firuze am 15 Jan. 2024
Beantwortet: Jona Grümbel am 19 Jan. 2024
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 Kommentare
Walter Roberson
Walter Roberson am 15 Jan. 2024
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 am 15 Jan. 2024
Bearbeitet: John D'Errico am 15 Jan. 2024
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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 15 Jan. 2024
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 am 15 Jan. 2024
Bearbeitet: Hassaan am 15 Jan. 2024
@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 Kommentare
Walter Roberson
Walter Roberson am 15 Jan. 2024
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 am 15 Jan. 2024
Bearbeitet: Hassaan am 15 Jan. 2024
Yes I agree one of many ways to do so.

Melden Sie sich an, um zu kommentieren.


Jona Grümbel
Jona Grümbel am 19 Jan. 2024
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);

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by