フィルターのクリア

Indexing through a cell array

1 回表示 (過去 30 日間)
Hari krishnan
Hari krishnan 2021 年 9 月 23 日
コメント済み: Jan 2021 年 9 月 23 日
I am looking to populate cell array named 'age_list' based on the values i have in two columns; named 'day' and 'pop'
  • For each specific 'day', i am looking at the corresponding 'pop' value and then update the 'age_list' based on it.
  • Example: On day 4, the population is 3. Therefore the cell array 'age_list' will have 'three' values with the {2,3,4}.
Can anyone suggest me on how to perform this? Any help will be appreciated.
  6 件のコメント
Jan
Jan 2021 年 9 月 23 日
編集済み: Jan 2021 年 9 月 23 日
Is day in all cases 1:numel(day) ? Can the population size shrink? Is {1} a fixed startpoint?
Hari krishnan
Hari krishnan 2021 年 9 月 23 日
Yes, day in all cases is 1:numel(day).
The population size cannot shrink.
{1} is a fixed starting point.

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

採用された回答

Jan
Jan 2021 年 9 月 23 日
A bold try:
day = 1:7;
pop = [1,2,3,3,3,4,6];
age_list = cell(1, numel(day));
age_list{1} = 1; % Is this given?!
for k = 2:numel(day)
new = pop(k) - pop(k - 1);
age_list{k} = cat(2, repmat(age_list{1}, 1, new), age_list{k-1} + 1);
end
age_list
age_list = 1×7 cell array
{[1]} {[1 2]} {[1 2 3]} {[2 3 4]} {[3 4 5]} {[1 4 5 6]} {[1 1 2 5 6 7]}
  2 件のコメント
Hari krishnan
Hari krishnan 2021 年 9 月 23 日
編集済み: Hari krishnan 2021 年 9 月 23 日
Thank you very much for the suggestion.
I would like to clarify an issue, if the length of days and pop is not equal, how will this work.
For example;
day = 1:178;
pop = [2,30,30,37,39,55,69,70,72,77,78,78,89,90,96,98,98,103,103,106,107,121,178];
Jan
Jan 2021 年 9 月 23 日
I cannot guess, what you expect as output in this case.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by