Effacer les filtres
Effacer les filtres

How to store output of iterative statement to a matrix?

1 vue (au cours des 30 derniers jours)
Rahman
Rahman le 21 Nov 2012
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]

Réponse acceptée

Jan
Jan le 21 Nov 2012
Modifié(e) : Jan le 21 Nov 2012
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);

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by