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
The discussion revolves around creating a MATLAB m-file to count occurrences of specific values (like zeros and ones) in a vector. The user has successfully obtained the length, maximum, and minimum values of the vector but struggles with counting individual elements. They provided a code snippet that attempts to implement this functionality but encounters an error due to incorrect syntax. The key issue highlighted is the need to use parentheses for indexing in MATLAB, specifically referencing elements with A(i) instead of A[i,1]. The conversation emphasizes correcting the indexing method and refining the loop to accurately count the occurrences of each value in the vector.
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).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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