Changing elements in a MxN matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
per isakson
2021년 4월 20일
편집: per isakson
2021년 4월 20일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!