 # Counting strcmp in GNU Octave

What is wrong with this little loop?

```aff = 0; # number of affecteds assigned
nor = 0; # number of normals assigned
# For definition of disease string matrix see "earlier in the program"
#####################################################################
# count affecteds
f = [1 : 1 : 150]; #integer counter and increment for status assignment
# start second while loop
while (f < 150)
if strcmp(disease(f),m) == (1)
aff = aff + 1;
break
else
nor = nor + 1;
break
endif
++f
endwhile

#---------------------------------------------------------
#earlier in the program
#m = ["affect"];
#n = ["normal"];
#disease(1) = n;
#disease(2) = m
#disease(3) = n
#disease(4) = m
#disease(5) = n
# etc.....
#from this little loop:
#####################################
# initialize fraction increment above minimum value
#########################
#f = [1 : 1 : 150]; #integer counter and increment for status assignment
# start first while loop
#while (f < 150) # (while loop a)
#if (f == (1))   # (if loop b)
#  disease(f) = n;
#endif
#if (rem (f, 2) == (0)) # (if loop c)
#  disease(f) = m;
#endif
#if (rem (f, 3) == (0)) # (if loop d)
#   disease(f) = n;
#endif
#list_in_columns ({disease(f)}, 6)
#++f
#endwhile
#################################
```

your use of te “break” command in both legs of the if statement means that it will only do the “while” loop once. ( I think)

so, I deleted the breaks, but I am still having trouble understanding the string variables because
when I give the interpreter this:

Without the disease “string matrix”, it is hard to tell but the way you increment your `f` vector and use it to index into the disease “string matrix” is also supiscious.

Hello Pantxo,

I supplied my “string matrix” in my initial comment in this thread. It is at the end of my code.
I will repeat it here because I don’t really understand your suggestion; also the interpreter is

What I don’t know is how to get my function f(x) to be the right size and length to compare with my standard g(x) and conclude they are the same or different at any given x.

specifically, I am now getting:
error: fri: =: nonconformant arguments (op1 is 150x1, op2 is 150x6)
when I try to compare disease(f) with a string function that can be represented by the regex ‘\w{6}’ that is 150 members (150x6). disease should be a function that is ‘\w6’ and that can be set in the program such that disease(f) where f = 1, 2, 3, 4, etc is also a function of 150x6. But, I am having trouble creating that. viz:
#snippet 1:
uu = {’\w{6}’};
disease = {’\w{6}’};
w = repmat (uu, 150, 6);
disease(f) = repmat (disease, 150, 6);

#snippet 2:
#(what I want to do here is set disease(f) to “normal”, a six character long string)
if (f == (1)) # (if loop b)
disease(f) = n; <-----this breaks the program giving the mismatch matrix error

Any help appreciated; string manipulation is often vexing. I code in perl, R, python and many other tongues.

As told in another topic not to be mean, but to save you a lot of time. Nobody is born programming Octave, but once you are familiarized with it, you can really play out the benefits of that language. Reading your code, I think ultimately you are trying to do something like this?

``````m = {"affect"};
n = {"normal"};
disease = [n m n m n];

# number of affecteds assigned
aff = sum (strcmp (disease, m))  # 2

# number of normals assigned
nor = sum (strcmp (disease, n))  # 3
``````

Octave, or Matlab (Matrix Laboratory) are about matrices and vectors (very different thinking from Python, C/C++, Java [I am not familiar with Perl and do little with R]). Writing for-loops in Octave (not in other languages) is “bad design” and should be only done if there is no vectorized operation possible.

Great comments! You are an excellent teacher!

I will work with what you told me as soon as I actually wake up.

It is 4AM here in California!

Bert