change even column values only

I create 20*20 2D array of all ones. And then I want to change even columns to minus one. How can I change with builtin function?

 Risposta accettata

Thorsten
Thorsten il 29 Set 2016
A = ones(20);
A(:,mod(1:20, 2) == 0) = -1;

2 Commenti

PK
PK il 29 Set 2016
good answer! I got the following method.... A = ones(20) A(:,2:2:end) = -1
Thorsten
Thorsten il 29 Set 2016
You method is even better!

Accedi per commentare.

Più risposte (1)

PK
PK il 29 Set 2016

0 voti

good answer! I got the following method.... A = ones(20) A(:,2:2:end) = -1

Tag

Richiesto:

PK
il 29 Set 2016

Commentato:

il 29 Set 2016

Community Treasure Hunt

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

Start Hunting!

Translated by