count of negative vector elements
조회 수: 60 (최근 30일)
이전 댓글 표시
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
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
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
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
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
You would use logical indexing: https://www.mathworks.com/help/matlab/math/array-indexing.html#MatrixIndexingExample-3
댓글 수: 0
Hassaan
2024년 1월 15일
편집: Hassaan
2024년 1월 15일
@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
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.
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!