Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
What is wrong with this
조회 수: 3 (최근 30일)
이전 댓글 표시
I just want to create a variable to hold 5 different sets of values...
Mass_red
Any ideas??
load remove_outlier.mat
LMT = [20 25 28 32 36];
HMT = [40 40 40 40 39.5];
for k=1:5
i=1;
while(Mass(i) < LMT(k))
i=i+1;
end
j=1;
while(Mass(j) < HMT(k))
j=j+1;
end
Mass_red(k) = Mass(i:j);
end
Thanks
Note: Mass is loaded in to the workspace. It is a 3201 x 1 double. Basically, it is a vector with sequential values from 15 to 47. I suppose you could say it was 15:0.01:47
댓글 수: 0
답변 (4개)
Andrei Bobrov
2012년 4월 13일
z = bsxfun(@times,Mass,bsxfun(@ge,Mass,LMT)&bsxfun(@lt,Mass,HMT));
Mass_red = z(any(z,2),:)';
OR [EDIT]
t = bsxfun(@ge,Mass,LMT)&bsxfun(@le,Mass,HMT);
z = bsxfun(@times,Mass,t)
Mass_red = arrayfun(@(ii)z(t(:,ii),ii),(1:size(z,2)),'un',0);
Thomas
2012년 4월 13일
To begin with:: you do not define Mass but you still use it to check its value in a while loop.
what is Mass?
EDIT
I think This is what you need
Mass=15:0.01:47;
LMT = [20 25 28 32 36];
HMT = [40 40 40 40 39.5];
for k=1:5
i=1
if (Mass(i) < LMT(k))
i=i+1
end
j=1
if (Mass(j) < HMT(k))
j=j+1
end
Mass_red(k,:) = Mass(i:j);
end
Walter Roberson
2012년 4월 13일
i:j is going to have varying range depending on the data values, so Mass(i:j) will be varying sizes. You are trying to store each iteration into a numeric array, but the iterations are not of constant size. That is going to be a problem: numeric arrays cannot have rows or columns of varying size. You are going to have to use a cell array:
Mass_red{k} = Mass(i:j);
Notice the {} instead of ()
댓글 수: 3
Walter Roberson
2012년 4월 13일
That error implies that you initialized Mass_red in some code you did not show us. You need to initialize it differently for cell arrays.
Image Analyst
2012년 4월 13일
Instead of all that looping over i and j, why not just do
LMTIndex = find(Mass >= LMT(k), 1, 'first');
HMTIndex = find(Mass >= HMT(k), 1, 'first');
startingIndex = min([LMTIndex HMTIndex]);
endingIndex = max([LMTIndex HMTIndex]);
Mass_red{k} = Mass(startingIndex : endingIndex)
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!