I have to make a matrix with 100 columns and 100 rows, I have to assign "2" to all even columns and "0" to odd columns

1 visualizzazione (ultimi 30 giorni)
Hi, I have to make a matrix with 100 columns and 100 rows, I have to assign "2" to all even columns and "0" to uneven columns. I also have to use for loops.
I've made this so far:
z = zeros(100);
[m , n ] = size(A)
for i=1:100
if mod(i,2)==0
z(:,i) = 2
end
end
But it creates for than 100 columns and rows and won's stop running, if I try to stop it, it opens up a new file named Workspacelistener.
What could I be doing wrong?
  1 Commento
KSSV
KSSV il 8 Set 2020
There is a typo error in your code, while getting sizes m,n you used A. Okay that m,n are not used next. You are taking output z(:,i) = 2 every time in a loop. This takes hell lot of time. You need to terminate the output with ;.
z = zeros(100);
[m , n ] = size(z)
for i=1:100
if mod(i,2)==0
z(:,i) = 2 ; % <------ terminate the output
end
end
You need not to use loop to acieve this. Check the answer.

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 8 Set 2020
A = zeros(100) ;
A(:,2:2:end) = 2 ;

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by