- There is "greater than" operator in MATLAB, which will operate on vectors
- There is also an operator that will sum over a vector
- There is a docsearch command that will search the documentation
How to count in arrays?
1 次查看(过去 30 天)
显示 更早的评论
Let's say I have two arrays
apple = [1 2 3 4 5 6 7 8];
pear = [2 3 1 2 7 6 1 8];
I want to compare these arrays and count how many items in pear are bigger than the item at the same place in apple. How would I do this?
1 个评论
the cyclist
2014-11-17
编辑:the cyclist
2014-11-17
Sounds like homework. Here are a few hints:
采纳的回答
Manoj
2014-11-17
x=sum(pear>apple);
1 个评论
John D'Errico
2014-11-17
While this correct, I recall that it will be slightly faster to use nnz instead of sum.
x = nnz(pear > apple);
The difference will be small unless the arrays are significantly large.
pear = round(rand(1,1e7)*10);
apple = round(rand(1,1e7)*10);
timeit(@() sum(pear > apple))
ans =
0.03888
timeit(@() nnz(pear > apple))
ans =
0.023221
更多回答(0 个)
另请参阅
类别
在 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!