フィルターのクリア

Need help creating a for loop

2 ビュー (過去 30 日間)
Tee-Kay
Tee-Kay 2019 年 6 月 18 日
編集済み: Jan 2019 年 6 月 18 日
Hello.
I need to create a loop that can do the following:
I have to create a distance matrix for plotting my graph. The total distance is 95 m with 5 m spaces. So it needs to start at 5 and end at 95. Start from 5 again till 90 and start from 5 till 85. 5 till 80 and so on.
It should look like this:
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 5 10 15 20 25 30 35 40 45 50 55 60 65 70 5 10 15 20 25 30 35 40 45 50 55 60 65 5 10 15 20 25 30 35 40 45 50 55 60 5 10 15 20 25 30 35 40 45 50 555 10 15 20 25 30 35 40 45 50 5 10 15 20 25 30 35 40 45 5 10 15 20 25 30 35 40 5 10 15 20 25 30 355 10 15 20 25 30 5 10 15 20 25 5 10 15 20 5 10 15 5 10 5
  2 件のコメント
Prasanth Sikakollu
Prasanth Sikakollu 2019 年 6 月 18 日
Is this the one you are looking for?
A =
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 0
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 0 0
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 0 0
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 0 0 0 0
5 10 15 20 25 30 35 40 45 50 55 60 65 70 0 0 0 0 0
5 10 15 20 25 30 35 40 45 50 55 60 65 0 0 0 0 0 0
5 10 15 20 25 30 35 40 45 50 55 60 0 0 0 0 0 0 0
5 10 15 20 25 30 35 40 45 50 55 0 0 0 0 0 0 0 0
5 10 15 20 25 30 35 40 45 50 0 0 0 0 0 0 0 0 0
5 10 15 20 25 30 35 40 45 0 0 0 0 0 0 0 0 0 0
5 10 15 20 25 30 35 40 0 0 0 0 0 0 0 0 0 0 0
5 10 15 20 25 30 35 0 0 0 0 0 0 0 0 0 0 0 0
5 10 15 20 25 30 0 0 0 0 0 0 0 0 0 0 0 0 0
5 10 15 20 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 10 15 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 10 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Tee-Kay
Tee-Kay 2019 年 6 月 18 日
Yes this what I am looking for

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

採用された回答

Guillaume
Guillaume 2019 年 6 月 18 日
No need for a loop:
result = nonzeros(flipud(tril(repmat((95:-5:5)', 1, 95/5))))'
Of course, if this is homework and you do need to use a loop, then we won't give you the solution but can tell you where you're going wrong if you show what you've done.
  1 件のコメント
Tee-Kay
Tee-Kay 2019 年 6 月 18 日
Hello, thank you so much it worked. It's not homework, I am working on my research project and my supervisor mentioned that I can write a loop for it that's why I thought I needed a loop. Thank you :)

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

その他の回答 (2 件)

vidhathri bhat
vidhathri bhat 2019 年 6 月 18 日
Hi
You can just use simple for loops to do this.
data = []
for i=95:-5:5
data = [data 5:5:i]
end
end
  1 件のコメント
Tee-Kay
Tee-Kay 2019 年 6 月 18 日
Thank you :)

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


Jan
Jan 2019 年 6 月 18 日
編集済み: Jan 2019 年 6 月 18 日
v = nonzeros((5:5:95).' .* fliplr(triu(ones(19)))).'

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by