count non zero data and reflect answers in a new array
2 views (last 30 days)
Show older comments
Steven Ross
on 10 Jan 2014
Commented: Steven Ross
on 10 Jan 2014
I have a Matrix consisting of three columns of data. What I want to do is create a fourth column of data derived from a scan down the third column. The fourth column data should reflect the data in column 3. For example if the data is a nonzero value then the fourth column is assigned a 1. However, if the data is zero then a count is initiated until the next nonzero value is reached and the final value of the count is then applied to fourth column. I have included a fourth column in the example data which I have done by hand to highlight my requirements but how can I do this in code?
Data =
0 0.002 1.65E-05 1 1 0.004 0 0 2 0.006 1.63E-05 2 3 0.008 0 0 4 0.01 0 0 5 0.012 0 0 6 0.014 0 0 7 0.016 0 0 8 0.018 0 0 9 0.02 0 0 10 0.022 0 0 11 0.024 0 0 12 0.026 0 0 13 0.028 0 0 14 0.03 1.60E-05 12 15 0.032 0 0 16 0.034 1.58E-05 2 17 0.036 1.55E-05 1 18 0.038 0 0 19 0.04 1.53E-05 2 20 0.042 0 0 21 0.044 1.48E-05 2 22 0.046 0 0 23 0.048 1.45E-05 2 24 0.05 1.43E-05 1 25 0.052 1.40E-05 1 26 0.054 1.38E-05 1 27 0.056 1.35E-05 1 28 0.058 1.33E-05 1 29 0.06 1.28E-05 1 30 0.062 1.25E-05 1 31 0.064 1.23E-05 1 32 0.066 1.18E-05 1 33 0.068 1.13E-05 1 34 0.07 1.10E-05 1 35 0.072 1.05E-05 1 36 0.074 1.03E-05 1 37 0.076 1.00E-05 1 38 0.078 9.75E-06 1 39 0.08 9.50E-06 1 40 0.082 9.25E-06 1 41 0.084 9.00E-06 1 42 0.086 0 0 43 0.088 8.75E-06 2 44 0.09 0 0 45 0.092 0 0 46 0.094 8.50E-06 3 47 0.096 0 0 48 0.098 0 0 49 0.1 0 0 50 0.102 0 0 51 0.104 8.25E-06 5
Accepted Answer
Image Analyst
on 10 Jan 2014
Try this:
clc;
Data =[...
0 0.002 1.65E-05 1
1 0.004 0 0
2 0.006 1.63E-05 2
3 0.008 0 0
4 0.01 0 0
5 0.012 0 0
6 0.014 0 0
7 0.016 0 0
8 0.018 0 0
9 0.02 0 0
10 0.022 0 0
11 0.024 0 0
12 0.026 0 0
13 0.028 0 0
14 0.03 1.60E-05 12
15 0.032 0 0
16 0.034 1.58E-05 2
17 0.036 1.55E-05 1
18 0.038 0 0
19 0.04 1.53E-05 2
20 0.042 0 0
21 0.044 1.48E-05 2
22 0.046 0 0
23 0.048 1.45E-05 2
24 0.05 1.43E-05 1
25 0.052 1.40E-05 1
26 0.054 1.38E-05 1
27 0.056 1.35E-05 1
28 0.058 1.33E-05 1
29 0.06 1.28E-05 1
30 0.062 1.25E-05 1
31 0.064 1.23E-05 1
32 0.066 1.18E-05 1
33 0.068 1.13E-05 1
34 0.07 1.10E-05 1
35 0.072 1.05E-05 1
36 0.074 1.03E-05 1
37 0.076 1.00E-05 1
38 0.078 9.75E-06 1
39 0.08 9.50E-06 1
40 0.082 9.25E-06 1
41 0.084 9.00E-06 1
42 0.086 0 0
43 0.088 8.75E-06 2
44 0.09 0 0
45 0.092 0 0
46 0.094 8.50E-06 3
47 0.096 0 0
48 0.098 0 0
49 0.1 0 0
50 0.102 0 0
51 0.104 8.25E-06 5];
% Get rid of first column:
Data(:,1) = [];
% Make 3rd column 1 if 2nd column is not 0
Data(:,3) = double(Data(:,2)~=0)
row = 1;
while row < size(Data, 1)
value = Data(row, 3);
if value == 0
% Start counting zeros.
zeroCount = 1;
while Data(row, 3) == 0
zeroCount = zeroCount + 1;
row = row + 1;
end
Data(row, 3) = zeroCount;
else
row = row + 1;
end
end
Data % Print to command window.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!