Creating a growing list

20 ビュー (過去 30 日間)
Saud Alfalasi
Saud Alfalasi 2020 年 12 月 20 日
コメント済み: Saud Alfalasi 2020 年 12 月 20 日
for r = 1 : whatever
for c = 1 : whatever2
if Range > 50
Joey = {('Bi**h Changed'),r,c}
Hi,
I want joey to be a list, growing, currently it's overwriting at index one.
Quick fix please, I've seen long winded methods but I'm sure there's something very simple.
Example:
Joey = {Joey,('Bi**h Changed'),r,c} which is silly. Or having a counter, Joey {counter} = ect..... counter = counter +1, which is also silly.

採用された回答

per isakson
per isakson 2020 年 12 月 20 日
編集済み: per isakson 2020 年 12 月 20 日
Add
Joey = cell(0);
before the loops and replace
Joey = {('Bi**h Changed'),r,c}
by
Joey(end+1:end+3) = {('Bi**h Changed'),r,c};
Silly or not is in the eyes of the beholder.
Or you might want
Joey{end+1} = {('Bi**h Changed'),r,c};
  1 件のコメント
Saud Alfalasi
Saud Alfalasi 2020 年 12 月 20 日
Joey(end+1:end+3) = {('Bi**h Changed'),r,c};
Hey man this is the best I could get, it displayed a horizontal list however beggers cant be choosers.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 12 月 20 日
編集済み: Image Analyst 2020 年 12 月 20 日
There are basically 2 ways that I know of and you listed them. I don't think they're silly. Using a counter is more efficient than appending (your first method). Using a table is even more efficient than a cell array. Even more efficient would be to allocate a large amount of empty cells - way more than you think you would need - then crop the array after the loop
Joey = cell(1000, 1)
counter = 1;
for r = 1 : whatever
for c = 1 : whatever2
if Range > 50
Joey(counter) = {('Bi**h Changed'),r,c}
counter = counter + 1;
end
end
end
Joey = Joey(1:counter); % Crop to however many we actually ended up needing.
  1 件のコメント
Saud Alfalasi
Saud Alfalasi 2020 年 12 月 20 日
Hi Image Analyst, I want to show you what I've produced after picking your brain (repeadly). Can I send you a file?
(just to show you, nothing else)(I think and hope you'll be impressed)
Saud

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by