Populate a 1000x1000 array with rows where a value is added every second, then third etc. column

10 ビュー (過去 30 日間)
I know how to make the individual rows:
% two = zeros (1,1000);
% two (2:2:end)=1;
and so on, but I can't do this for every row (this might be a roundabout way of doing it). How do I incorporate this in a loop? Preferably directly into an array. I've been trying for hours, but I haven't used matlab in years and was never the best at using it beyond graphs.
The array should look something like this:
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1
0 0 1 0 0 1 0 0 1 0
0 0 0 1 0 0 0 1 0 0
(but for 1000x1000)
I somehow always end up with my array being just 1s when I try to incorporate it in a loop.
I've been trying to do some puzzles to get back into using matlab, and I managed fine for one that needed similar things, but with if/else; but for loops (which I think I need here) were always my kryptonite, so please help me! Thank you!
  4 件のコメント
CaraAkame
CaraAkame 2022 年 4 月 28 日
Alright, so I think I tried what you're getting at dpb, at some earlier point. It kept giving me 1s, but someone answered what might have been the issue.
I wanted to post the failing code, but I could not for the life of me remember what I did hours ago, and at this point it's just a mess of commented out lines that are not connected.
It was something like this (which when I run it still gives me 1s, but I have not tried the other solution yet):
% A=zeros(1000);
% eggs=(1:1:1000)';
% for n = eggs(:,1)
% A(n:n:end)=1;
% end
CaraAkame
CaraAkame 2022 年 4 月 28 日
I know what I did wrong now, thank you all for your help!

サインインしてコメントする。

採用された回答

Jan
Jan 2022 年 4 月 28 日
編集済み: Jan 2022 年 4 月 28 日
You want an output of size 1000x1000:
M = zeros(1000, 1000);
You want a loop to go from 1 to 1000:
for k = 1:1000
...
end
You want to set some elements to 1 in the k'th row of M:
M(k, a:b:c) = 1;
Here c is obviously 1000. Just find matching values for a and b.
You find an exhaustive tutorial here:
  1 件のコメント
CaraAkame
CaraAkame 2022 年 4 月 28 日
Thank you!
This was really helpful in understanding what my mistake was, since I did most of that (forgot the k), and overcomplicated other stuff (the k again), on instinct and half remembered coding sessions from uni, and could not figure out why it wasn't working. Thank you for taking the time to explain this in steps!

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2022 年 4 月 28 日
If you were getting a bunch of ones, you were probably pretty close.
N = 1000;
everyN = zeros(N);
for n = 1:N
everyN(n,n:n:end) = 1;
end
If you forgot the row index, the first pass would have set the entire array to 1.
  1 件のコメント
CaraAkame
CaraAkame 2022 年 4 月 28 日
Oh wow, that looks very similar to what I did the first time around, I'm going to go try that right now. If I was really that close for the last few hours I'm going to be both proud and annoyed at myself!
.
.
It really was my biggest error. I mean, my starting statement for the for loop was also slightly wonky, but I'm sure I had the statement that way as well at some point as well. Thank you so much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by