How to produce an array that displays the common data values in other arrays?

1 回表示 (過去 30 日間)
Jacey
Jacey 2024 年 6 月 4 日
コメント済み: Jacey 2024 年 6 月 4 日
I am trying to find a way to make a resulting array that displays all of the common values (in the same row/colomn) that multiple arrays share. I attached an example photo to explain what I mean. So, as you can see in the photo, I am trying to produce the resulting array called "Total" by using the shared values from A, B, and C.

採用された回答

Stephen23
Stephen23 2024 年 6 月 4 日
編集済み: Stephen23 2024 年 6 月 4 日
A = [1,1,0;1,1,1;0,1,1]
A = 3x3
1 1 0 1 1 1 0 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = [1,0,1;0,1,0;1,1,1]
B = 3x3
1 0 1 0 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = [1,1,1;1,1,0;1,1,1]
C = 3x3
1 1 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D = double(A & B & C)
D = 3x3
1 0 0 0 1 0 0 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (1 件)

Matlab Pro
Matlab Pro 2024 年 6 月 4 日
Your question is not clear enogh
I have changed a,b &c to hold also other values than 0,1
The result is :
  • "ind" holding the indexes of matching values and
  • "common" - holding the actual common values
a = [5 1 0;1 22 1;0 0 3];
b = [5 0 1;0 22 0; 1 0 3];
c = [5 1 1;1 22 0;1 0 3];
a_and_b = a==b;
b_and_c = b==c;
a_and_b_and_c = a_and_b == b_and_c;
ind = find(a_and_b_and_c)
ind = 6x1
1 2 4 5 6 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
common = a(a_and_b_and_c)
common = 6x1
5 1 1 22 0 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
Matlab Pro
Matlab Pro 2024 年 6 月 4 日
.. and to get the total (logical):
a = [5 1 0;1 22 1;0 0 3];
b = [5 0 1;0 22 0; 1 0 3];
c = [5 1 1;1 22 0;1 0 3];
a_and_b = a==b;
b_and_c = b==c;
a_and_b_and_c = a_and_b == b_and_c;
ind = find(a_and_b_and_c);
total = false(size(a));
total(ind) = true;
Unrecognized function or variable 'a'.

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

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by