How to multiply special elements

Hi,
I am a new with Matlab and I need some help. I need to multiply by (-1) all the elements that their row is even and their column is odd or their row is odd and their column is even. How can I do that?
Thanks you in advance

댓글 수: 3

James Tursa
James Tursa 2014년 11월 7일
What have you done so far? Please post your current code and then we can comment on it and offer suggestions. There are a lot of ways to do this. Do you know how to write a for loop? Do you know how to determine if a row number is even or odd? Are you familiar with the function bsxfun?
Image Analyst
Image Analyst 2014년 11월 7일
If this is a homework problem you should have Tagged it as homework below. If so, tag it and read this.
Brian
Brian 2014년 11월 7일
Our teacher told us that we can use the function ind2sub. I know that I can use mod but i dont know after to put a condition. A[i,j] if ([(i+j) mod 2]==0) A[i,j]=-A[i,j]

답변 (2개)

Chad Greene
Chad Greene 2014년 11월 7일

0 개 추천

Here's one way:
A = magic(5)
[cols,rows] = meshgrid(1:size(A,1),1:size(A,2));
A(mod(rows,2)==0&mod(cols,2)==1 | mod(cols,2)==0&mod(rows,2)==1)=-A(mod(rows,2)==0&mod(cols,2)==1 |mod(cols,2)==0&mod(rows,2)==1)

댓글 수: 1

James Tursa
James Tursa 2014년 11월 7일
Homework is easy these days ...
Image Analyst
Image Analyst 2014년 11월 7일

0 개 추천

Simply use numel and linear indexing:
m=randi(255, 5, 6) % Create sample data.
indexesToInvert = 2 : 2 : numel(m);
m(indexesToInvert) = -m(indexesToInvert)

댓글 수: 1

Brian
Brian 2014년 11월 7일
I think that if the size of the matrix is odd it's not working

이 질문은 마감되었습니다.

태그

질문:

2014년 11월 7일

마감:

2021년 8월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by