How to store output of iterative statement to a matrix?

1 次查看(过去 30 天)
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by