creating partition of sets

1 次查看(过去 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 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by