I am trying to create this matrix ; How can i do this using for loops or without loops?
1 回表示 (過去 30 日間)
古いコメントを表示
A=[0 0 0 0 0 0 0 0 0 ; 1 1 1 0 0 0 0 0 0 ; 1 1 1 1 1 1 0 0 0 ];
回答 (1 件)
John D'Errico
2022 年 4 月 7 日
編集済み: John D'Errico
2022 年 4 月 7 日
Without loops? Trivial.
A=[0 0 0 0 0 0 0 0 0 ; 1 1 1 0 0 0 0 0 0 ; 1 1 1 1 1 1 0 0 0 ];
A
As you can see, that works nicely. Why would you need any other way? Yes. There are surely ways you could do it. But why would you use loops? Yes, I suppose you could do this:
B = ones(3,9);
B(1,1) = 0;
B(2,4) = 0;
B(3,7) = 0;
B = cummin(B,2);
B
Or you might have done this:
C = zeros(3,9);
C(2,1:3) = 1;
C(3,1:6) = 1;
C
And I could easily come up with a million other ways.
But if you are looking for some elegant, magical way to solve the problem, when a perfectly good, inelegant solution exists, don't bother.
The art of programming is NOT about elegance, not about beauty. The art of programming is to get the job done. NOW. Don't waste your time looking for an elegant solution when an inelegant one exists that is perfectly adequate. Only if the solution you have is too slow or a problem for some reason do you look further.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!