a simple c matrix problem of counting number based on one of the dimension's content.[C language]
Now I get a 5-d matrix which M[j][i][l][ref][0]...
In this matrix, j,i belong to 0~4, while l=0开发者_JAVA技巧. ref can vary from 0 to 4.
I just want to count the number according to ref's value.
For example,
- if ref =0, I will do count0++,
- if ref =1, count1++,
- if ref =2, count2++
- ...
So, I do not care what is the value of M, I just want to know the appearing frequency with different ref value.
Thanks.
You're not quite clear on how what you want to do depends on the actual contents of M -- I'm guessing this contains some sort of variable count you want to add to your individual counts?
In this case, how about something like this:
int j, i, ref;
int counts[5];
for(ref=0; ref<5; ref++)
counts[ref]=0;
for(j=0; j<4; j++)
for(i=0; i<5; i++)
for(ref=0; ref<5; ref++)
counts[ref]+=M[j][i][0][ref][0];
(I have replaced your individual "count" variables with an array, which makes things much easier.)
Edit: I just saw that you don't care what the contents of M are. In that case, I don't really understand what you're trying to do. Since the dimensions are constant, the number of entries that exist for a certain value of ref
are always constant, and are always the same for all ref
-- in this case, 5*5=25, since you have five entries along each of the j and i dimensions.
If this is not what you want, please clarify.
精彩评论