The function gfrank(A,p) in matlab is supposed to return the rank of a matrix A in GF(p) where p is prime.
But it seems that there is something wrong with this function. as it returns impossible values for some matrices.
For example for the 10*9 matrix:
0 1 1 1 1 0 1 0 0
1 0 0 1 0 1 0 1 1
0 1 1 0 1 1 1 0 0
0 1 1 0 1 1 1 0 1
0 1 0 0 0 0 0 0 1
0 0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 1 1
0 1 0 1 0 1 0 1 0
0 1 1 1 1 0 0 1 1
in GF(29), the function returns 10, which is impossible as it is larger than one of the dimensions.
The example is found using this code:
M = 10;
pP = [];
Y =[];
for P = 19:100
if isprime(P)
P
pause(3)
for i = 1:100
A=round(rand(M,M-1));
pP = [pP,P];
if(gfrank(A,P) > M-1) % A IS AN (M-1)*M MATRIX.
disp(A); % IF RANK OF A IS GREATER THAN OR
disp(gfrank(A,P)); % EQUAL TO M, WE HAVE AN ERROR!
end
end
end
end
Related
I want to create a matrix which which has:
The value 1 if the row is odd and the column is odd
The value 1 if the row is even and the column is even
The value 0 Otherwise.
I want to get the same results as the code below, but in a one line (command window) expression:
N=8;
A = zeros(N);
for row = 1:1:length(A)
for column = 1:1:length(A)
if(mod(row,2) == 1 && mod(column,2) == 1)
A(row,column*(mod(column,2) == 1)) = 1;
elseif(mod(row,2)== 0 && mod(column,2) == 0 )
A(row,column*(mod(column,2) == 0)) = 1;
end
end
end
disp(A)
This is the expected result:
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
A simple approach is to use implicit expansion of addition, noting that
odd+odd = even+even = 0
So this is your answer:
A = 1 - mod( (1:N) + (1:N).', 2 );
You could also do this with toeplitz, as shown in this MATLAB Answers Post
For a square matrix with number of rows = number of columns = N
A = toeplitz(mod(1:N,2));
If the number of rows (M) is not equal to the number of columns (N) then
A = toeplitz(mod(1:M,2),mod(1:N,2))
FWIW, you're asking a specific case of this question:
How to generate a customized checker board matrix as fast as possible?
Can you take three lines?
N=8;
A = zeros(N);
A(1:2:end, 1:2:end) = 1;
A(2:2:end, 2:2:end) = 1;
One line solution (when N is even):
A = repmat([1, 0; 0 1], [N/2, N/2]);
You can try the function meshgrid to generate mesh grids and use mod to determine even or odd
[x,y] = meshgrid(1:N,1:N);
A = mod(x+y+1,2);
such that
>> A
A =
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
I have a binary array and I would like to flip values based on the length which they repeat. as an example
Ar = [0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
Ideally I would like to flip the 1's which repeat only 2 or fewer times resulting in the following.
Ar = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
From what I have located online, the Diff function is most commenly used to locate and remove sequences. But from what I have located, it appears to target all instances.
Simply use imopen from Image Processing toolbox with a kernel of 3 ones -
imopen(Ar,[1,1,1])
Sample run -
>> Ar = [0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
>> out = imopen(Ar,[1,1,1]);
>> [Ar(:) out(:)]
ans =
0 0
1 0
0 0
0 0
0 0
1 0
1 0
0 0
0 0
0 0
1 1
1 1
1 1
1 1
1 1
0 0
0 0
0 0
0 0
1 1
1 1
1 1
1 1
1 1
1 1
Vectorized solution without using I.P. toolbox -
function out = filter_islands_on_length(Ar, n)
out = Ar;
a = [0 Ar 0];
d = diff(a);
r = find(d);
s0 = r(1:2:end);
s1 = r(2:2:end);
id_arr = zeros(1,numel(Ar));
m = (s1-s0) <= n;
id_arr(s0(m)) = 1;
id_arr(s1(m)) = -1;
out(cumsum(id_arr)~=0) = 0;
Sample runs -
>> Ar
Ar =
0 1 0 0 0 1 1 0 0 0 1 1 1
>> filter_islands_on_length(Ar, 2)
ans =
0 0 0 0 0 0 0 0 0 0 1 1 1
>> filter_islands_on_length(Ar, 1)
ans =
0 0 0 0 0 1 1 0 0 0 1 1 1
Another solution requiring no toolbox but needs Matlab 2016a or later:
n = 3; % islands shorter than n will be removed
Ar = movmax(movsum(Ar,n),[ceil(n/2-1) floor(n/2)])==n;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The matrix A contains 1's in different coordinates:
A =
1 0 0 0 1 0 0 0 0 0
0 1 0 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 0 1
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
Step 1:
Finding the coordinates of the 1's. For example, in the first row it is (1,1) and (1,5).
c1 = find(A==1)
Step 2:
Scanning this coordinates in Main M matrix and performing AND operation. If the answer is 1 then place the 1 in corresponding coordinates of A matrix. For example, (1,1) (1,5) in M matrix is ANDed with (2,1)(2,5)==> 1 1 ANDed 0 0 ==>0 0. Likewise (3,1) (3,5) upto (10,1) (10,5) in M matrix. If any place 1 it came place the 1 in respective coordinate place in A matrix.
M =
1 0 0 0 1 1 1 1 1 1
0 1 0 0 0 1 1 1 1 1
0 0 1 0 1 1 1 1 1 1
0 0 0 1 0 0 0 0 1 1
1 0 1 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 1 1
1 1 1 0 0 0 1 0 1 1
1 1 1 0 0 0 0 1 0 0
1 1 1 1 0 1 1 0 1 0
1 1 1 1 0 1 1 0 0 1
Here in the given matrix in 4th row A matrix has 1 in (4,4) check the remaining coordinates in M matrix. It is ANDed with (1,4) the (2,4), while (9,4) it is 1. Place that 1 in A matrix (4,9). I have tried with the code but it is not working in generic case.
a = 1:size(M)
R1 = 1;
for j = 1:size(A)
A1 = A(j,:)
c = find(A1==1) % finding 1's place
l = length(c)
a1 = a(a~=j)
for k = a1(1):a1(end)
R1 = 1;
for i = 1:l1
temp1 = R1
R1 = and(M(j,c(i)),M(k,c(i))) % performing AND operations
R2 = and(R1,temp1)
end
if (R2==1) % if the condition is satisfied by 1
A(j,k)=1 % place the 1 in the particular coordinate in A matrix
end
end
end
New_A = A
New_A =
1 0 0 0 1 0 0 0 0 0
0 1 0 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 0 1
0 0 0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1 0 0
If I understand your question, then for each row r of A you want to extract all M columns where A(r,:)==1, then place 1 in first column c of A(r,:) that all the columns in row c in the extracted M is 1. Using the updated A continue the process on the same row until you reach the end, and move to the next row.
Here is a code for that (assuming size(A,1)==size(M,2)):
new_A = A; % copy the current state
for r = 1:size(new_A,1)
Mc = M(:,new_A(r,:)==1).'; % extract the relevat columns in new_A
c = 1; % column counter
while c<=size(A,2)
next_item = find(all(Mc(:,c:end),1),1)+c-1; % find the next item to change
new_A(r,next_item) = 1; % if all rows in Mc are 1, then place 1
Mc = M(:,new_A(r,:)==1).'; % extract the relevat columns in new_A
c = c+1; % go to the next column
end
end
which result in new_A using your A and M above:
new_A =
1 0 0 0 1 0 0 0 0 0
0 1 0 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 0 1
0 0 0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1 0 0
Is this what you looked for?
I want to create a matrix of size m-by-n where all elements in a column are 0 except one element which is 1. That one element must be at a random position.
eg.
[0 1 0 0 0
0 0 1 0 0
1 0 0 1 0
0 0 0 0 0
0 0 0 0 1]
To add some variety, here's another approach:
m = 4;
n = 5;
[~, result] = sort(rand(m,n));
result = double(result==1);
This gives, for example,
result =
0 0 0 0 1
0 1 0 0 0
1 0 0 1 0
0 0 1 0 0
You can also use rand and max to do the job:
m=4;
n=5;
R=rand(m,n);
result = bsxfun(#eq, R, max(R,[],1))
On my machine it gave:
1 1 0 0 0
0 0 0 0 0
0 0 1 0 1
0 0 0 1 0
How it works: Generating a random matrix, R, and then setting to 1 the entry corresponding to the maximal element at each column. No need for sorting.
Regarding the original answer of Divakar, since it uses randperm it is restricted to square matrix only, and it will only produce random permutation matrices.
One possible way to correct his solution is to use randi instead of randperm:
result = bsxfun( #eq, (1:m)', randi(m, 1, n ) )
May give this output:
1 0 1 0 0
0 0 0 0 0
0 0 0 0 0
0 1 0 1 1
As for the answer of bla, using accumarry can save the use of zeros and sub2ind:
m=5; n=10;
R=randi(m,n,1);
A = accumarray( {R, (1:n)' }, 1, [m n] )
May give this output:
0 0 0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0 1 0
1 0 0 1 0 0 0 0 0 1
0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0
Another idea I have is to create the identity matrix of size m x m, then use randi with a range from 1 up to m to create a vector of n elements long. After, you'd use this vector to access the columns of the identity matrix to complete the random matrix you desire:
m = 5; n = 5; %// Given your example
M = eye(m);
out = M(:,randi(m, n, 1));
Here's one possible run of the above code:
out =
1 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
0 1 1 0 1
here's an example using randi:
m=5; n=10;
A=zeros(m,n);
R=randi(m,n,1);
A(sub2ind(size(A),R',1:n))=1
A =
0 0 0 0 0 0 0 1 0 1
0 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 1 0 1 0
You can use sparse with randi for a one-liner, like so -
full(sparse(randi(m,1,n),1:n,1,m,n))
Sample run -
>> m = 5; n = 6;
>> full(sparse(randi(m,1,n),1:n,1,m,n))
ans =
0 1 0 0 0 1
0 0 1 1 0 0
0 0 0 0 0 0
1 0 0 0 1 0
0 0 0 0 0 0
I have a vector of n size, which I would like to transform into the Boolean matrix of nxm, where m is a number of unique values in that vector.
a = repmat(1:5:20,1,3)
a =
1 6 11 16 1 6 11 16 1 6 11 16
The result I would like to have is the matrix 12x4:
1 0 0 0
0 1 0 0
0 0 1 0
...
0 0 0 1
Any ideas how to do that without for loop?
You can try this:
a = repmat(1:5:20,1,3);
b = unique(a);
bsxfun(#eq, a', b)
The result would be:
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
a = repmat(1:5:20,1,3)
b=unique(a);
c = repmat(a',1,numel(b))== repmat(b,numel(a),1);
but in general a loop will be faster, repmat is to be avoided.
So, now with a loop:
a = repmat(1:5:20,1,3)
b=unique(a);
c=false(numel(a),numel(b));
for ii=1:numel(a)
c(ii,:) = a(ii)==b;
end