Changing elements in a MxN matrix
1 view (last 30 days)
Show older comments
I am trying to get zeros for certain part of a Matrix to zeros
I need this matrix ones that are bolded, underlines, italic "1" to become zeros. I have been trying to figure this out for a while. I tried if statesments for row > 5 and for loops.
4 1 0 1 0 1 0 1 0 1
1 4 1 0 1 0 1 0 1 0
0 1 4 1 0 1 0 1 0 1
1 0 1 4 1 0 1 0 1 0
0 1 0 1 4 1 0 1 0 1
1 0 1 0 1 4 1 0 1 0
0 1 0 1 0 1 4 1 0 1
1 0 1 0 1 0 1 4 1 0
0 1 0 1 0 1 0 1 4 1
1 0 1 0 1 0 1 0 1 4
Here is my code:
inverse = diag(ones(10,1));
inverse = inverse*4;
for col = 1:length(domain-1)
for row = 1:10
if row == col
inverse(row+1,col) = 1;
inverse(row,col+1) = 1;
elseif mod(row+col,2) == 1
inverse(row,col) = 1;
end
end
end
matrix = inverse(Nx-1,Ny-1)
0 Comments
Accepted Answer
per isakson
on 20 Apr 2021
Edited: per isakson
on 20 Apr 2021
Does this help?
%%
matrix = magic(10);
matrix = triu( matrix,-4 );
matrix = tril( matrix, 4 )
%% This is better, it only overwrites the specific diagonals
matrix = magic(10);
for k = [5,7,9]
isdiag = diag( true(10-k,1), k );
matrix( isdiag ) = 0;
isdiag = diag( true(10-k,1), -k );
matrix( isdiag ) = 0;
end
matrix
I was fooled by your example and used a 10x10 matrix in my demos. Your title says MxN matrix.
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!