How to count here?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Stephen23
2015-1-22
编辑:Stephen23
2015-1-22
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 个)
David Sanchez
2015-1-22
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!