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
AI Thread 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).
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top