For loop with changing matrix

2 ビュー (過去 30 日間)
Bas
Bas 2015 年 8 月 14 日
コメント済み: blaat 2015 年 8 月 18 日
Hi,
I have a for loop (j= 1:years) in which matrix A changes per j. Matrix A is a matrix containing birthdates in column 8. Each row represents a person. Per year the total number of persons decreases, so every j+1, the rows in matrix A reduces.
I want to generate a new matrix B, representing the age of each person w.r.t. t= today. So matrix B should be of length(A), though this changes over j.
The output I want to get is a matrix B ,including all ages per j, so j columns.
Can anyone help me with this? I tried several codes, but it's not working...

回答 (1 件)

blaat
blaat 2015 年 8 月 14 日
You could use a cell array to store the ages per j. For example:
B = cell(1, years);
for j = 1:years
% A is computed here
B{j} = <operations on A(:, 8) to calculate age>;
end
Is this what you mean?
  2 件のコメント
Bas
Bas 2015 年 8 月 14 日
Thanks, this works. But now I want to check for every i= 1:length(A) of matrix B per j= 1:years if the value equals a value in matrix C. How can I compare this cell array with a normal matrix?
blaat
blaat 2015 年 8 月 18 日
I'm assuming here that C has a single value per year j. You could then compare the value in a loop over the years:
for j = 1:years
is_equal = B{j} == C;
% Do things with is_equal here...
end
Alternatively, you could use cellfun().

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

カテゴリ

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