Odd/Even If statement for a N by M Matrix
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
I have a N by M matrix and I am trying to assign numbers to even row,cols. For example (2,2); (4,4); ...; etc = 10 and (1,1);(3,3); ...; etc = 5. 
Later on N by M will change. More stuff will be calcuated inbetween. It has to be in a if statement. 
x=10;
y=10;
matrix = zeros(x,y);
for col = 1:y
    for row = 1:x
        % For Even
        if row  == isodd
            matrix (row,col) = 10;
        % For Odd
        else 
            matrix (row,col) = 5;   
        end
    end
end
댓글 수: 0
채택된 답변
  Thukiller
      
 2021년 4월 15일
        like this?
x=10;
y=10;
matrix = zeros(x,y);
for col = 1:y
    for row = 1:x
        % For Even
        if mod(row,2)  == 0
            matrix (row,row) = 10;
        % For Odd
        else 
            matrix (row,row) = 5;   
        end
    end
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

