How to multiply special elements
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
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 Commenti
James Tursa
il 7 Nov 2014
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
il 7 Nov 2014
If this is a homework problem you should have Tagged it as homework below. If so, tag it and read this.
Brian
il 7 Nov 2014
Risposte (2)
Chad Greene
il 7 Nov 2014
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 Commento
James Tursa
il 7 Nov 2014
Homework is easy these days ...
Image Analyst
il 7 Nov 2014
Simply use numel and linear indexing:
m=randi(255, 5, 6) % Create sample data.
indexesToInvert = 2 : 2 : numel(m);
m(indexesToInvert) = -m(indexesToInvert)
1 Commento
Brian
il 7 Nov 2014
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!