フィルターのクリア

How to store output of iterative statement to a matrix?

1 回表示 (過去 30 日間)
Rahman
Rahman 2012 年 11 月 21 日
I have the following code segment
numRows=size(rulesTable,1);
fluCertainity=[-1 100 100 75 20 25 80];
for i=1:numRows
if rulesTable(i,1)==headache && rulesTable(i,2)==temperature
CF=fluCertainity(i)
end
end
Question: I want to store the fluCertainity value into the matrix 'CF' if the condition is true. How can I take CF as a matrix to store all the values from fluCertainity in case of true cases?
Thanks in advance. Rahman Ali
[EDITED, code formatted, Jan]

採用された回答

Jan
Jan 2012 年 11 月 21 日
編集済み: Jan 2012 年 11 月 21 日
k = 0;
CF = zeros(numRows); % Pre-allocate
for i=1:numRows
if <your condition>
k = k + 1;
CF(k) = fluCertainity(i);
end
end
CF = CF(1:k); % Crop unneded memory
Or vectorized - no need for a loop:
index = (rulesTable(:,1)==headache & rulesTable(:,2)==temperature);
CF = fluCertainity(index);
  1 件のコメント
Rahman
Rahman 2012 年 11 月 21 日
Thanks Jan. It worked.

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

その他の回答 (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