필터 지우기
필터 지우기

How many times each element of an array have the 1 value after n times assigned different logical values?

조회 수: 1 (최근 30일)
I have n same size matrices containing different logical values such as A1, A2, A3 matrices. It also can be considered that a matrix_A was assigned n times with different logical values 1 and 0. Please help to point out how to count the number of times each elements of this A matrix having 1 value. Any suggestion would be much appreciated!
A1 = 1 0 0 1 0 1 0
0 1 0 1 0 0 0
0 1 1 0 0 1 0
0 1 1 0 1 0 1
A2 = 1 0 0 0 0 1 0
0 1 0 0 0 0 0
0 1 1 1 0 1 0
0 1 1 1 1 0 1
A3 = 0 0 0 0 0 1 1
0 1 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 1
  댓글 수: 1
Stephen23
Stephen23 2018년 9월 21일
편집: Stephen23 2018년 9월 21일
Numbered variables are a sign that you are doing something wrong. It is much more efficient to use one array (which could be numeric, cell, struct, table, etc) and indexing.

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

채택된 답변

Stephen23
Stephen23 2018년 9월 21일
편집: Stephen23 2018년 9월 21일
Simple: use one 3D array:
A = cat(3,...
[1 0 0 1 0 1 0
0 1 0 1 0 0 0
0 1 1 0 0 1 0
0 1 1 0 1 0 1],...
[1 0 0 0 0 1 0
0 1 0 0 0 0 0
0 1 1 1 0 1 0
0 1 1 1 1 0 1],...
[0 0 0 0 0 1 1
0 1 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 1]);
And then:
>> sum(A,3)
ans =
2 0 0 1 0 3 1
0 3 1 2 1 0 0
0 2 2 1 0 2 0
0 2 2 1 2 0 3
Note: if your arrays are in one cell array C then you can easily conver this into a 3D array:
A = cat(3,C{:});
If you have lots of separate variables, then you need to fix your code design.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2018년 9월 21일
Constructing your data as 3d matrix will make it easy.
A(1,:,:) =[ 1 0 0 1 0 1 0
0 1 0 1 0 0 0
0 1 1 0 0 1 0
0 1 1 0 1 0 1];
A(2,:,:) = [1 0 0 0 0 1 0
0 1 0 0 0 0 0
0 1 1 1 0 1 0
0 1 1 1 1 0 1];
A(3,:,:) = [0 0 0 0 0 1 1
0 1 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 1];
m=4;
n=5;
sum(A(:,m,n))

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by