count of negative vector elements
60 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 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
on 15 Jan 2024
Edited: John D'Errico
on 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
As you can see, the less than or equal to operator, used and written in MATLAB as <= will tell you that information directly.
Answers (3)
Walter Roberson
on 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
You would use logical indexing: https://www.mathworks.com/help/matlab/math/array-indexing.html#MatrixIndexingExample-3
0 Comments
Hassaan
on 15 Jan 2024
Edited: Hassaan
on 15 Jan 2024
@Firuze Few pointers(hints) to help you get started and ease your MATLAB journey.
- Logical Indexing: Use logical conditions to create a "mask" that identifies which elements of your vector meet certain criteria (e.g., being negative).
- 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.
- Relational Operators: Use < to check for negative elements and >= to check for positive elements or zeros.
- 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 Comments
Walter Roberson
on 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.
Jona Grümbel
on 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);
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!