Matrix which is 1 when row and column are both odd or both even - matlab

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

Related

Iterating through a matrix using a smaller matrix

I've been struggling with this for a bit now. I have a small matrix s for example and a bigger matrix B as shown below.
B =
0 0 0 0 0 0 1 1
1 1 0 0 1 0 1 1
1 1 0 1 0 0 1 1
1 1 1 0 0 0 1 0
0 0 1 1 1 0 0 1
0 0 0 1 1 1 1 1
1 1 1 0 0 0 1 0
0 1 1 0 1 1 0 0
s =
1 1
1 1
What I want to do is iterate through B with s and compare the values. If all the values in s equal the values in B (the small section of B), then the answer is 1, if not then 0.
The 1's and 0's would be placed in a matrix as well.
This is what I've done so far but unfortunately, it doesn't iterate step by step and doesn't create a matrix either.
s = ones(2,2)
B = randi([0 1],8,8)
f = zeros(size(B))
[M,N]=size(B); % the larger array
[m,n]=size(s); % and the smaller...
for i=1:M/m-(m-1)
for j=1:N/n-(n-1)
if all(s==B(i:i+m-1,j:j+n-1))
disp("1")
else
disp("0")
end
end
end
Any help would be appreciated!
The following code works on the examples you supplied, I haven't tested it on anything else, and it will not work if the dimensions of the smaller matrix are not factors of the dimensions of the larger matrix, but you didn't indicate that it needed to do that in your description.
B =[0 0 0 0 0 0 1 1
1 1 0 0 1 0 1 1
1 1 0 1 0 0 1 1
1 1 1 0 0 0 1 0
0 0 1 1 1 0 0 1
0 0 0 1 1 1 1 1
1 1 1 0 0 0 1 0
0 1 1 0 1 1 0 0];
S =[1 1
1 1];
%check if array meets size requirements
numRowB = size(B,1);
numRowS = size(S,1);
numColB = size(B,2);
numColS = size(S,2);
%get loop multiples
incRows = numRowB/numRowS;
incCols = numColB/numColS;
%create output array
result = zeros(incRows, incCols);
%create rows and colums indices
rowsPull = 1:numRowS:numRowB;
colsPull = 1:numColS:numColB;
%iterate
for i= 1:incRows
for j= 1:incCols
result(i,j) = isequal(B(rowsPull(i):rowsPull(i)+numRowS-1, colsPull(j):colsPull(j)+numColS-1),S);
end
end
%print the resulting array
disp(result)

Matlab - Shuffling matrix values based on some conditions

I have the following two matrices which are outputs of a procedure. The size of the matrices may change but both matrices will always be the same size: size(TwoHopMat_1) == size(Final_matrix)
Example:
TwoHopMat_1 =
0 0 0 0 1
0 0 1 1 0
0 1 0 1 0
0 1 1 0 0
1 0 0 0 0
Final_matrix =
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 0 0 0
1 0 0 0 1
Now I need to shuffle the final_matrix such that i meet the following conditions after shuffling:
Every column should have a minimum of one 1s
If i have a 1 in a particular position of TwoHopMat_1 then that particular position should not have 1 after shuffling.
The conditions should work even if we give matrices of size 100x100.
first step: set one element of each column of the result matrix ,that is not 1 in Final_matrix ,to 1
second step: then remaining ones randomly inserted into positions of the result matrix that are not 1 in Final_matrix and are not 1 in the first step result
TwoHopMat_1=[...
0 0 0 0 1
0 0 1 1 0
0 1 0 1 0
0 1 1 0 0
1 0 0 0 0];
Final_matrix=[...
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 0 0 0
1 0 0 0 1];
[row col] = size(Final_matrix);
result = zeros(row ,col);
%condition 1 & 2 :
notTwoHop = ~TwoHopMat_1;
s= sum(notTwoHop,1);
c= [0 cumsum(s(1:end - 1))];
f= find(notTwoHop);
r = floor(rand(1, col) .* s) + 1;
i = c + r;
result(f(i)) = 1;
%insert remaining ones randomly into the result
f= find(~(result | TwoHopMat_1));
i = randperm(numel(f), sum(Final_matrix(:))-col);
result(f(i)) =1
A possible solution:
function [result_matrix] = shuffle_matrix(TwoHopMat_1, Final_matrix)
% Condition number 2
ones_mat = ones(size(TwoHopMat_1));
temp_mat = abs(TwoHopMat_1 - ones_mat);
% Condition number 1
ones_to_remove = abs(sum(sum(temp_mat)) - sum(sum(Final_matrix)));
while ones_to_remove > 0
% Random matrix entry
i = floor((size(Final_matrix, 1) * rand())) + 1;
j = floor((size(Final_matrix, 2) * rand())) + 1;
if temp_mat(i,j) == 1
temp_mat(i,j) = 0;
ones_to_remove = ones_to_remove - 1;
end
end
result_matrix = temp_mat;
end
Note: this solution uses brute force.

Matrix row/column manipulation in matlab

I have got the following function for spreading out the number of 1's in a matrix and if there are rows with all 0's or all 1's then that particular row has to be deleted
function ReducedMatrix = ReduceMatrix(result)
D1 = sum(result(:));
NumberOfOnes = floor(D1*0.3);
NewMatrix = zeros(size(result));
NewMatrix(randi(numel(NewMatrix),1,NumberOfOnes)) = 1;
ReducedMatrix = NewMatrix;
while numel(ReducedMatrix)/numel(NewMatrix) > 0.2
IndexOfFullRows = find(all(ReducedMatrix));
if isempty(IndexOfFullRows)
break
end
ReducedMatrix(:,IndexOfFullRows(1)) = [];
end
end
The input of the function and output are as follows
result =
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 0 1 1
1 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 1 0 1
1 0 1 1 1 1 1 0 1 1
1 1 1 1 0 1 1 1 0 1
1 0 1 1 1 0 1 1 1 1
1 1 1 1 0 1 0 1 1 1
1 1 1 0 1 1 1 1 1 1
ReducedMatrix =
0 1 1 0 0 0 0 0 1 0
0 1 0 0 0 0 0 1 0 0
1 1 1 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 1 1
1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 1
row_sum =
3
2
3
2
1
4
2
2
0
3
col_sum =
3 4 4 1 0 0 3 2 2 3
Now if there exists a row or column with the row_sum/col_sum equal to either 0 or 1 then then the corresponding row has to be deleted.
For Example. Row-R4,R9 and Col-C4,C5,C6 have row_sum and col_sum as either 1,0. So adding them up R4,R9,C4,C5,C6 = 5 rows have to be eliminated from the matrix so my reduced matrix should be of the size 5x5. Please note column should not be eliminated and instead of removing columns having 0 and 1, the corresponding rows can be removed. Similarly this function has to run for larger matrices with the same constraints. I tried doing the above function however i do not possess enough skills to achieve my desired results, Any help is much appreciated
I see a number of potential problems and possible simplifications to your code.
For one thing, the way you construct the original matrix, NewMatrix(randi(numel(NewMatrix),1,NumberOfOnes)) = 1; may not behave the way you would expect. randi does not guarantee that the same index will not appear multiple times in the output, so your new matrix may have fewer ones than the original. To solve this, shuffle the elements using randperm:
ReducedMatrix = [ones(1, NumberOfOnes), zeros(1, numel(result) - NumberOfOnes)];
ReducedMatrix = ReducedMatrix(randperm(numel(ReducedMatrix)));
ReducedMatrix = reshape(ReducedMatrix, size(result));
Secondly, you do not need to construct the new matrix as NewMatrix and then reassign it with ReducedMatrix = NewMatrix;. Just do ReducedMatrix = zeros(size(result)); and skip the reassignment. For the while loop condition, where NewMatrix appears to be "used", remember that numel(NewMatrix) == numel(result).
If you are not removing homogeneous columns, only rows, you do not need a loop to do the removal:
rowSum = sum(ReducedMatrix, 2);
rowMask = (rowSum == size(ReducedMatrix, 2) | rowSum == 0);
ReducedMatrix(rowMask, :) = [];
Your original code seems to swap the row and column indices when removing the rows. It also did not handle the case of all zeros. If you want to remove not more than 30% of rows, you can do something like this before the removal:
rowMask = find(rowMask); % Convert to indices
rowMask = rowMask(1:min(numel(rowMask), round(0.3 * size(ReducedMatrix, 2))));

How to perform logical AND operation by refering the coordinates in matrix [closed]

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?

How to replace duplicate elements as 0 in column matrix in matlab

I need to replace the repeated elements in column of a matrix as 0's and delete the rows which has all 0's. If my matrix is like this means.
Input =
1 0 0 1
0 1 0 1
0 0 1 1
1 1 1 1
My expected output should be like this
Output =
1 0 0 1
0 1 0 0
0 0 1 0
0 0 0 0 ---> this row should be get deleted in this case
This doesn't work for my problem
c = [ 1 1 0 1 0 1 1 1 0 1 1 0];
[c, ic] = unique(a, 'first');
c(~ismember(1:length(a),ic)) = 0;
You can use logical indexing and cumsum:
A = [1 0 0 1;
0 1 0 1;
0 0 1 1;
1 1 1 1];
ind = cumsum(A); %cumulative sum (by column)
A(ind>1) = 0;
A(sum(A')==0,:)=[]