Filter löschen
Filter löschen

How to store output of iterative statement to a matrix?

1 Ansicht (letzte 30 Tage)
Rahman
Rahman am 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]

Akzeptierte Antwort

Jan
Jan am 21 Nov. 2012
Bearbeitet: Jan am 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);

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by