Check elements of matrix
이전 댓글 표시
i have this program
N=1000000;
t=1;
X=rand(1,N);
for i=0:.1:5
y=(X>=i & X<(i+.1));
s(t)=sum(y);
t=t+1;
end
-----------------------------------
so; i need to check all elements of matrix X against each element in matrix i. is there any method to do this without for loop? i.e. by using matrix notation.
채택된 답변
추가 답변 (2개)
Azzi Abdelmalek
2012년 12월 9일
편집: Azzi Abdelmalek
2012년 12월 9일
N=1000000;
X=rand(1,N)*6;
arrayfun(@(x) sum(X(X>x & X<x+1)),1:5)
Greg Heath
2012년 12월 9일
0 개 추천
Matrices can be compared using <, =, or > only if they have the same dimensions or at least one is a scalar.
Note that
X < 1
Therefore
y = 0 for i > 1.
Consequently, the following makes more sense:
t=0
for i=0:0.1:1 % NOT 5
t =t + 1 % Update at beginning of loop
y = ...
s(t) = ...
end
Hope this helps.
Thank you for formally accepting my answer.
Greg
P.S. An alternative is to use 0: 0.1 :5 with X = 5*rand(N)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!