Calculation of probality numbers from "matrix/array"

1 回表示 (過去 30 日間)
Oliver Bubniak
Oliver Bubniak 2020 年 10 月 1 日
コメント済み: Oliver Bubniak 2020 年 10 月 8 日
Hello, I just start coding in Matlab and I would like to calculate probability number. f.e
A=[1 7 1 2 8 8 6 3 7 7]
then, I would like to know probability of number 8 or 6 etc. "A" could be array for example.
Is any function for this or is possible to use any simple loop to find this.
Thank you.

回答 (1 件)

Raunak Gupta
Raunak Gupta 2020 年 10 月 5 日
Hi,
You can count number of element equal to a particular number in a vector/matrix using nnz and equality condition. For above query you can find first for 8 and then for 6 and add the two and divide by total number of elements in a vector/matrix.
numOf8 = nnz(A==8);
numOf6 = nnz(A==6);
probOf8or6 = (numOf8 + numOf6)./numel(A);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by