Change matrix bits depending on the rate (Matlab)

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 Comments

I read the text 3 times and do not understand, what this means: "verify if the final bits depending on the rate are 0 or 1." What is a "final bit"? How does a bit depend on which rate? What exactly is "permutation between rows and columns"?
This is not celar to me also: "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"
@Jan a rate of 1/2 means in the matrix I make a path of 1 row and 2 columns as indicated in the figure of the matrix. If for example I have a rate of 2/5, I make a path of 2 rows and 5 columns each time.
So as you said, I have to make permutation between rows and columns to obtain 1 in place of 0. The role of permutation here is to save the aqual number of ones in each row and column.
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)?
@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.

Sign in to comment.

 Accepted Answer

AMO
AMO on 21 May 2021
Edited: AMO on 21 May 2021
[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 Comments

@AMO It's not really what I want. The number of ones in each row and column doesn't still the same
@Afluo Raoual I run the code with the matrix you provided and it doesn't change the number of ones in rows or columns. Infact this code just swaps two randomly selected rows or columns, which doesn't change the number of ones, until a solution is found.
input
A =
0 0 1 1 1 0 1 1 1 0
0 1 1 1 0 1 0 0 1 1
1 1 1 0 0 1 1 1 0 0
1 0 0 1 1 0 0 1 1 1
1 1 0 0 1 1 1 0 0 1
output
A =
0 1 1 1 0 1 0 0 1 1
0 0 1 1 1 0 1 1 1 0
1 1 0 1 0 1 1 1 0 0
1 0 1 0 1 0 0 1 1 1
1 1 0 0 1 1 1 0 0 1
@AMO I run it too, but the result is
output
A =
1 1 0 1 0 0 0 1 0 1
0 0 0 1 1 1 1 1 0 0
0 1 0 0 1 1 1 0 1 1
1 0 1 0 0 1 1 1 1 0
1 0 1 1 0 0 0 0 1 1
Input matrix has 3 ones in each column and 6 ones in each row. But output matrix has sometimes 3 ones in each column or 2 ones, also has 5 ones in each row or 6. So here the number of ones is not equal as in input matrix.
@Afluo Raoual Can you run it with seed like
rng(1)
post your result?
@AMO I didn't understand your meaning !!!
I said that when I run it, the result is not correct. The number of ones in each row and column is not the same, and I have posted the result in the previous comment
@Afluo Raoual I have seen your result. I am trying to figure out why your answer has different number of ones. If you add rng(1) at the top line then we will have the same results. Can you add rng(1) and post the result again?
@AMO I don't understand you meaning by adding rng(1) at the top line.
Do you mean in the beginning of the program ?
If you can repost the correct code here please !
@Afluo Raoual yes beginning of the program.
@AMO Sorry, the result is always wrong. Number of ones is not the same
@AMO This is the program that I used to run
clear;clc;
rng(1);
A=[0 0 1 1 1 0 1 1 1 0;
0 1 1 1 0 1 0 0 1 1;
1 1 1 0 0 0 1 1 0 0;
1 0 0 1 1 0 0 0 1 1;
1 1 0 0 1 1 1 0 0 0];
[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
And the result is :
A =
1 1 0 0 0 1 0 0 1 1
1 1 1 1 1 0 1 0 0 0
0 0 0 1 1 1 1 0 1 0
0 0 1 1 1 0 0 1 1 1
0 1 1 0 0 1 0 1 0 1
@Afluo Raoual from the beginning your A matrix is missing ones in rows 3, 4 and 5. The number of ones in those rows are 5 not 6. If you correct that the code will work.
@AMO It works now. Thank you

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 20 May 2021

Commented:

AMO
on 1 Jun 2021

Community Treasure Hunt

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

Start Hunting!