Reputation: 160
I have a 161*32 matrix (labelled "indpic") in MATLAB and I'm trying to find the frequency of a given number appearing in a row. So I think that I need to analyse each row separately for each value, but I'm incredibly unsure about how to go about this (I'm only new to MATLAB). This also means I'm incredibly useless with loops and whatnot as well.
Any help would be greatly appreciated!
Upvotes: 0
Views: 1434
Reputation: 7929
You can just write
length(find(indpic(row_num,:)==some_value))
and it will give you the number of elements equal to "some_value" in the "row_num"th row in matrix "indpic"
Upvotes: 0
Reputation: 32930
If you want to count the number of times a specific number appears in each row, you can do this:
sum(indpic == val, 2)
where indpic
is your matrix (e.g image) and val
is the desired value to be counted.
Explanation: checking equality of each element with the value produces a boolean matrix with "1"s at the locations of the counted value. Summing each row (i.e summing along the 2nd dimension results in the desired column vector, where each element being equal to the number of times val
is repeated in the corresponding row).
If you want to count how many times each value is repeated in your image, this is called a histogram, and you can use the histc
command to achieve that. For example:
histc(indpic, 1:256)
counts how many times each value from 1 to 256 appears in image indpic
.
Upvotes: 2
Reputation: 624
Like this,
sum(indpic(rownum,:) == 7)
obviously change 7 to whatever.
Upvotes: 0