operation in matrix in matlab

1 Ansicht (letzte 30 Tage)
ali hassan
ali hassan am 31 Jan. 2022
Bearbeitet: Arif Hoq am 31 Jan. 2022
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.

Akzeptierte Antwort

Arif Hoq
Arif Hoq am 31 Jan. 2022
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 Kommentare
Arif Hoq
Arif Hoq am 31 Jan. 2022
to find index try with this:
[row,col]=find(a>4);
Arif Hoq
Arif Hoq am 31 Jan. 2022
Bearbeitet: Arif Hoq am 31 Jan. 2022
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 31 Jan. 2022
Bearbeitet: Stephen23 am 31 Jan. 2022
"...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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by