필터 지우기
필터 지우기

I want to implement all possible 2*2 or 3*3 or n*n matrices by using 0 or/and 1 as their elements.

조회 수: 1 (최근 30일)
I want to implement all possible 2*2 or 3*3 or n*n matrices by using 0 or/and 1 as their elements.

채택된 답변

Jan
Jan 2022년 2월 25일
편집: Jan 2022년 2월 26일
n = 3;
M = rem(uint8(floor((0:2^(n*n)-1) .* pow2(0:-1:1-n^2).')), 2);
M = reshape(M, n, n, []);
M(:, :, 1)
ans = 3×3
0 0 0 0 0 0 0 0 0
M(:, :, 2)
ans = 3×3
1 0 0 0 0 0 0 0 0
M(:, :, 512)
ans = 3×3
1 1 1 1 1 1 1 1 1
This is growing extremely fast. After n=7 2^(n*n) leave the range of accurate integer values. For n=6 you need 550 GB RAM. Unfortunately I do not find a way to create this array directly as UINT8 in pure Matlab. Otherwise 69 GB RAM would be sufficient for n=6.
For standard computers, the function is bearable for n=1 to 5 only. It does not seem to be worth to write a C-Mex function for this job.
  댓글 수: 4
Jan
Jan 2022년 2월 25일
Almost, @Bruno Luong, if dec2bin() would create the array a little bit smarter (R2021b):
% s = char(ones(numel(d),numBits)*48);
Intermediately a double array is created before it is converted to char. What a pity.
Worth to send an enhancement request to MathWorks.
Did you take a look into the code to extract the bits of the uint64 values in dec2bin?
It would be smart, if we can use bitget with implicit expanding and replying uint8 instead of doubles:
bitget((0:2^(n*n)-1).', 1:n, 'uint8') % Not working!!!
Bruno Luong
Bruno Luong 2022년 2월 25일
No I did not look at the code dec2bin.
After taking a look fromm your attention, yeah the implementation seems not particularly cheap in term of memory and speed.

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

추가 답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 2월 25일
Looks like 16 combinations of 4 values for the 2x2, and 2^9 combinations of 9 values for the 3x3. Try using dec2bin and reshape in a for loop, and post code and specific questions if you run into problems.

카테고리

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