Compare two columns under 3 conditions

2 ビュー (過去 30 日間)
Francisco Anaya
Francisco Anaya 2019 年 4 月 2 日
コメント済み: Francisco Anaya 2019 年 4 月 2 日
Hi,
I an array (phases) with two vector of the same lenght. I need to compare the two vectors under the following conditios:
phases(:,1) phases(:, 2)
1................................. 2
1................................. 2
1................................. 2
2................................. 2
2................................. 2
2................................. 1
2................................. 1
2................................. 1
2................................. 1
1................................. 1
1................................. 1
1................................. 1
condition 1: I want to know the number of times phases(:,1) and phases(:, 2) are both 2 in the same row. In this case, there are 2 times.
condition 2: I want to know the number of times phases(:, 2) is equals to 2 when phases(:,1) is equals to 1 in the same row. In this case, there are 3 times.
condition 3: I want to know the number of times phases(:, 1) is equals to 2 when phases(:, 2) is equals to 1 in the same row. In this case, there are 4 times.
Thank you!
  3 件のコメント
Francisco Anaya
Francisco Anaya 2019 年 4 月 2 日
yes, they are same...
following your example below, then it should be....?
nnz(phases(:,2) == 2 & phases(:,2) == 2)
nnz(phases(:,1) == 1 & phases(:,2) == 2)
nnz(phases(:,1) == 2 & phases(:,2) == 1)
Francisco Anaya
Francisco Anaya 2019 年 4 月 2 日
it works only for condition 2 and 3...
phases= [1 1 1 2 2 2 2 2 2 1 1 1; 2 2 2 2 2 1 1 1 1 1 1 1]';
>> nnz(phases(:,2) == 2 & phases(:,2) == 2)
ans =
5
>> nnz(phases(:,1) == 1 & phases(:,2) == 2)
ans =
3
>> nnz(phases(:,1) == 2 & phases(:,2) == 1)
ans =
4

サインインしてコメントする。

回答 (1 件)

Shunichi Kusano
Shunichi Kusano 2019 年 4 月 2 日
The following code counts the number of rows which meet the condition 2.
nnz(phases(:,2) == 2 & phases(:,1) == 1)
hope this helps.

カテゴリ

Help Center および File ExchangeSwitches and Breakers についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by