필터 지우기
필터 지우기

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);

추가 답변 (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