Populate a 1000x1000 array with rows where a value is added every second, then third etc. column
Show older comments
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 Comments
As you've outlined that you're practicing, let's go at it as if were homework and provide hints to aid in learning, not just a canned solution.
Solution 1: Your above will work (remember to preallocate and address for performance, of course) in a loop; what if you make the step size in colon a variable???
Jan
on 28 Apr 2022
"I somehow always end up with my array being just 1s" - then the best strategy in the forum is to post the failing code, such that the readers can point to the problem and fix it.
CaraAkame
on 28 Apr 2022
CaraAkame
on 28 Apr 2022
Accepted Answer
More Answers (1)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!