Change matrix bits depending on the rate (Matlab)

1 回表示 (過去 30 日間)
Afluo Raoual
Afluo Raoual 2021 年 5 月 20 日
コメント済み: AMO 2021 年 6 月 1 日
Dear members
I have matrix A of dimensions s1*s2 where the number of ones in each column and row is equal
And I have to write a program that verify if the final bits depending on the rate are 0 or 1. If final bits depending on the rate is 0, I have to make permutation between rows and columns in order to obtain final bits 1 not 0 and save the equal number of ones in each row and column.
For example : I have a matrix 5*10, so the rate is 5/10=1/2 and the number of ones in each row is 6 and in each column is 3
The final bit depending on the rate 1/2 in the first row is 0, so here I have to make permutation between rows and columns to obtain 1 in place of 0 and the number of ones in each row and columns still the same. We obtain this matrix finally :
I tried to program the first part that calcul the rate of the matrix like that:
[s1, s2] = size(A);
d = gcd(s1, s2);
n1 = s1 / d;
n2 = s2 / d;
Can you help me to program the permutation please.
  4 件のコメント
Jan
Jan 2021 年 5 月 21 日
I still do not understand, what "permutation between rows and columns" means. Do you exchange some transposed columns with rows or the other way around? Do you want to permute the elements of a specific row and column? Do you want to change the order of rows and/or columns? Should the permutation randomly or do you want to swap the specific value with any (or the last) matching different value of the row (or column)?
Afluo Raoual
Afluo Raoual 2021 年 5 月 21 日
@Jan Permutation means change the order of rows and/or columns randomly until that all the final bits (zeros) depending on the rate ( red bits in the matrix ) being all ones with condition that the number of ones in each column and row still the same. For example in this matrix we have 3 ones in each colulmn and 6 ones in each row. It must still the same.

サインインしてコメントする。

採用された回答

AMO
AMO 2021 年 5 月 21 日
編集済み: AMO 2021 年 5 月 21 日
[s1, s2] = size(A);
d = gcd(s1, s2);
n1 = s1 / d;
n2 = s2 / d;
% rows and columns of final bits
rows = 1:s1;
cols = zeros(1,s1);
cOffset = 0;
for c = n2:n2:s2
cols((1:n1) + cOffset) = c;
cOffset = cOffset + n1;
end
% linear index of final bits
finalBits = sub2ind([s1,s2], rows, cols);
% untill all final bits are 1s
while any(A(finalBits) == 0)
% randomly select to swap either rows or colmns
if randi(2) == 1
% swap rows
rswap = randperm(s1,2);
A(rswap,:) = A(flip(rswap),:);
else
% swap columns
cswap = randperm(s2,2);
A(:,cswap) = A(:, flip(cswap));
end
end
  13 件のコメント
Afluo Raoual
Afluo Raoual 2021 年 6 月 1 日
@AMO It works now. Thank you
AMO
AMO 2021 年 6 月 1 日
@Afluo Raoual You're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by