MATLAB: Ho to implement Sets in matlab

sets

hey everybody,, how to use Matlab in the set. problem: A = {1,2}, A ^ 2 = {(1,1), (1,2), (2,2) (2,1)};
how to find A ^ 100 by using Matlab?

Best Answer

P = 100;
A = uint8([1, 2]); %if your stored values exceed 255 then change the uint8 to suit
lenA = length(A);
if lenA <= intmax('uint8')
sstype = 'uint8';
elseif lenA <= intmax('uint16')
sstype = 'uint16';
elseif lenA <= intmax('uint64')
sstype = 'uint64';
else
error('A is too large to be able to subscript into. How did you even manage to create it??');
end
num_AP = lenA^P;
try
AP = zeros(num_AP, P, class(A));
catch
whoA = whos('A');
bytes_per_entry = whoA.bytes ./ lenA;
A_bytes_required = num_AP * P * bytes_per_entry;
error('You need to install more memory; you need at least %g bytes of memory', A_bytes_required);
end
T = zeros(1, P, sstype);
for J = 1 : num_AP
AP(J,:) = A(T+1);
for K = P : -1 : 1
if T(K) ~= lenA - 1
T(K) = T(K) + 1;
break;
else
T(K) = 0;
end
end
end
On my system, the maximum that can be constructed is for P = 28, which needs about 6.7 gigabytes.
As far as I know, there are no implementations of the x64 architecture that have more than 48 memory pins. In the case where A is a matrix with only 2 elements, that would give a maximum of P = 42, because 2^43 * 43 entries * 1 byte/entry > 2^48 bytes. However, I wrote the code assuming that you have access to a classified computer with memory capacities exceeding 2*10^9 times as much information stored on Earth as of 2013. (Don't tell me if I am wrong -- I am not authorized to know.)
The code was designed to reduce the memory requirements, not for efficiency.
Related Question