How to count here?

1 view (last 30 days)
Chathu
Chathu on 22 Jan 2015
Commented: Chathu on 22 Jan 2015
Here is what i originally have:
'No.' 'Time'
'2011' '119.823011000'
'2012' '119.923206000'
'2013' '120.023454000'
'2014' '120.122663000'
'2015' '120.222851000'
'2016' '120.323050000'
'2017' '120.422283000'
'2018' '120.522470000'
'2019' '120.622677000'
'2020' '120.721896000'
'2021' '120.822124000'
'2022' '120.922356000'
'2023' '121.021516000'
'2024' '121.121760000'
'2025' '121.221989000'
In the Time column, i want to find the count till time < 121.000000000.
This is what i wrote:
No=floating(data{2}<121.000000000);
count=sum(~No)
But i got an error.

Accepted Answer

Stephen23
Stephen23 on 22 Jan 2015
Edited: Stephen23 on 22 Jan 2015
You code is almost right, just the function floating is not known to MATLAB. It doesn't exist, unless you have written it yourself, and using it probably gives you an error ("Undefined function..."). But that is alright, the data class of data{2} values is not really very important anyway, as the < operator is defined for all numeric classes anyway, as well as logical and character arrays. Try this:
idx = data{2} < 121;
sum(~idx)
The documentation states the applicable data types for the lt operator. Tip: Have a look at the "Contents" panel on the left-hand side of the webpage. Explore it for a few minutes: this is a really great way to find related functions, and to explore what you can do with particular classes of data. Get comfortable with using these "Contents" and using MATLAB becomes a lots more fun and interesting!
  1 Comment
Chathu
Chathu on 22 Jan 2015
@ Stephen- it works. Thank you so much.
In addition, thanks alot for the extra piece of information.

Sign in to comment.

More Answers (1)

David Sanchez
David Sanchez on 22 Jan 2015
If you want to sum over the second column of your cell array untill the value of the cell is equal or greater to 121:
Since you have to start counting from your second row (first row is the variable name)
your_sum = 0; % initialize your summation
for k=2:size(your_cell,1)
if str2double(your_cell{k,2})>= 121
break
end
your_sum = str2double(your_cell{k,2}) + your_sum;
end
  1 Comment
Chathu
Chathu on 22 Jan 2015
@ David- thanks alot for your response. Can you kindly tell me what does "your_cell" refers in your For loop? i guess it's the time column,correct?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!