필터 지우기
필터 지우기

creating partition of sets

조회 수: 5 (최근 30일)
k khaja
k khaja 2021년 2월 16일
편집: k khaja 2021년 2월 18일
I am getting error, can anyone correct it please. Thanks in advance.
%A partition of a set A is a finite or infinite collection of nonempty, mutually disjoint subsets whose union is A.
A = [0, 1, 2, 3, 4]
C = {}; D = [];
j = 1;
while j < 10
P = input('Enter INTEGERS with [ ] around them');
D = cell2mat(C)
if ismember(P,A) == ones(1,length(P)) && ismember(P,D) ~= ones(1,length(D))
C{(end+1)} = P;
else
disp('Entered wrong, Please Enter correct one\n');
j = j;
end
j = j + 1;
end

답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 16일
if ismember(P,A) == ones(1,length(P)) && ismember(P,D) ~= ones(1,length(D))
P is a vector. ismember() is going to return a vector the same length. == comparison to ones is going to return a vector the same length. You cannot use && with vectors
You could perhaps replace the first test with
if all(ismember(P, A))
Your second test is harder to make sense of as your documentation does not make clear what you want to have happen if some entries match but others do not. I seem to be having difficulty translating your documentation as to what the program is intended to do or how it is intended to do it.
My guess:
if all(ismember(P, A)) && ~any(ismember(P, D))
  댓글 수: 1
k khaja
k khaja 2021년 2월 18일
편집: k khaja 2021년 2월 18일
To create a partition, first I have to check the main set A, whether all are from this set and second test will check whether the same elements already choosen for any other other partion C, and I converted C to single matrix D for comparison purpose.
Thanks a lot Walter, your guess is right and It worked, this is what I want.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by