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 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
KSSV
2020 年 9 月 8 日
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.
回答 (1 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!