if i want to know the number of times an condition is fullfilled for a matrix, how can i do so;
a=[3 4 2 5 3 5]; %defining a matrix
% now what should i write if i want to know the number of times, a matrix
% has a value greater than 4.

 채택된 답변

Arif Hoq
Arif Hoq 2022년 1월 31일

0 개 추천

a=[3 4 2 5 3 5];
expected_value=a(find(a>4))
expected_value = 1×2
5 5
how_many_times= length(find(a>4))
how_many_times = 2
Or
times=length(expected_value)
times = 2

댓글 수: 4

ali hassan
ali hassan 2022년 1월 31일
what if i want to find that which entry is greater than 4
where = find(a>4)
Note: @Arif Hoq could also have written
expected_value = a(a>4);
the find() is not necessary in that context.
to find index try with this:
[row,col]=find(a>4);
Arif Hoq
Arif Hoq 2022년 1월 31일
편집: Arif Hoq 2022년 1월 31일
a=[3 4 2 5 3 5];
expected_value=a(a>4)
expected_value = 1×2
5 5
how_many_times= length(find(a>4))
how_many_times = 2
[row,col]=find(a>4)
row = 1×2
1 1
col = 1×2
4 6

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2022년 1월 31일
편집: Stephen23 2022년 1월 31일

0 개 추천

"...i want to know the number of times, a matrix has a value greater than 4."
The efficient MATLAB approach:
a = [3,4,2,5,3,5];
nnz(a>4)
ans = 2

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2022년 1월 31일

편집:

2022년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by