How can I count the number of zero elements in a vector using MATLAB?

  • Thread starter Thread starter Fert
  • Start date Start date
  • Tags Tags
    File Matrix
Click For Summary
SUMMARY

The discussion focuses on counting the number of zero elements in a vector using MATLAB. The user attempts to create a function that calculates the length, maximum, minimum, and count of zero elements in a vector. The provided code snippet contains syntax errors, particularly in the element referencing and conditional statements. The correct method to reference elements in MATLAB is using parentheses, as demonstrated in the suggested solution.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with vector operations in MATLAB
  • Knowledge of control flow structures such as loops and conditionals
  • Experience with MATLAB function creation
NEXT STEPS
  • Learn how to use MATLAB's built-in functions like sum for counting specific elements
  • Explore MATLAB's logical indexing for efficient data manipulation
  • Study MATLAB's error handling to debug code effectively
  • Investigate MATLAB's vectorization techniques to optimize performance
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly students and professionals in engineering or data analysis, who need to manipulate and analyze vector data efficiently.

Fert
Messages
6
Reaction score
0
I'm trying to write an m-file in MATLAB that can count how many data point in a vector are zero, how many are 1, etc. I can call the vector and get the length, max and min values, but I also need to be able to call each element seperatly and I can't figure out how to do this. I've been playing around and this is what i have:

function len (A)
N= length(A);
M =max(A);
m=min(A);
S=0;
for i=1:N

if
A[i,1]=0;
S=S+1;
end



len=N
MAX =M
MIN=m
zero=S

But it says expression (A=[i,1]=0) is incomplete. I've tried a few other expressions but none work. Any help is appreciated.
 
Technology news on Phys.org
When you reference an element in Matlab, you use parenthesis, if I remember correctly. So you'd need to reference A(i,1).
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
10
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K