Clear Filters
Clear Filters

I want to add a column that counts the passes for this test. When the values in the first column are between 0-300 seconds, I want to consider that pass 1, etc.

1 view (last 30 days)
I want to add a column that counts the passes for this test. When the values in the first column are between 0-300 seconds, I want to consider that pass 1, etc.
I figured out how to add a new column:
%Add new columnm to mark passes
passestest= zeros(size(CETR,1),1);
CETR=[CETR,passestest];
I would then like to find the averages for each of the passes (averages of all rows in pass 1).

Answers (2)

Walter Roberson
Walter Roberson on 25 Jan 2018
Possibly you want to use histcounts(), passing in edges like [0 300 450 ....] to count 0 <= x < 300, 300 <= x < 450, and so on
  2 Comments
Marian Kennedy
Marian Kennedy on 25 Jan 2018
Edited: Walter Roberson on 25 Jan 2018
Is there a way to automate this process?
for i=1:numel(CETR1(:,1)) %numel means number of elements in CETR1(:,1)
if CETR1(i,1)<300
CETR1(i,12)=1;
elseif CETR1(i,1)<600
CETR1(i,12)=2;
elseif CETR1(i,1)<900
CETR1(i,12)=3;
end
end
Steven Lord
Steven Lord on 25 Jan 2018
The histcounts function returns bin counts as its first output and bin membership numbers in its third output. The discretize function returns bin membership numbers in its first output. In this case I'm going to get histcounts tell me what edges it used and reuse those edges in discretize. If you know the bin edges you want to use, you can specify them in either histcounts or discretize.
x = randperm(100, 25);
[bincounts1, edges1, binlocations1] = histcounts(x)
binlocations2 = discretize(x, edges1)
isequal(binlocations1, binlocations2) % true
From your description you're more interested in binlocations1 (or binlocations2, since they're the same) than bincounts1.

Sign in to comment.


Guillaume
Guillaume on 25 Jan 2018
Edited: Guillaume on 25 Jan 2018
You can do the discretisation (as that's what you want is called) and the addition as a new column all in one go:
CETR1(:, end+1) = discretize(CETR1(:, 1), [0 300 600 900])
No loop needed of course.

Categories

Find more on Oceanography and Hydrology 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!