How to extract rows which contains specific values in each row, then automatically repeat the process for a range.
조회 수: 1 (최근 30일)
이전 댓글 표시
i have a table of values imported from excel where one of the columns has a range of 0 to 8 . where there are several rows associated to each number through 0 and 8 . how can i extract the rows that have 0 in that column to do calulations , then repeat the proccess with the rows that have 1 in the same column and so on. would it require a loop ? how would the code be setup ?
as seen below all the results are te same because the calulations are being done to the whole column rather than the specified ones in red ( which should be equal to no_avg)
댓글 수: 4
Bob Thompson
2019년 6월 10일
Are you running into a specific error message? If so, please post the entire error message.
If not, please explain why you think the code is not doing what you want it to. What results are you seeing, and how do they differ from what you would expect?
채택된 답변
Adam Danz
2019년 6월 10일
편집: Adam Danz
2019년 6월 11일
The reason you're getting the same values is because "X" always equals the same thing:
X = t1{:,{'x0102ThrottleNotch'}};
n=0:8;
X(any(X == 0),:) % <-------------- this doesn't do anything. You're not assigning the value to a variable
for notch = n
throttle_notch = X; %< --------- this is always t1{:,{'x0102ThrottleNotch'}}
no_avg = mean(throttle_notch);
throttle_notch_mode = mode(throttle_notch);
throttle_notch_min = min(throttle_notch);
throttle_notch_max = max(throttle_notch);
T = table(no_avg)
end
Instead you want something like this
X = t1{:,{'x0102ThrottleNotch'}};
for notch = 0:8 %<----- loop through 0:8
throttle_notch = X(any(X==notch),:); %<----- change the row indices on each loop
no_avg = mean(throttle_notch);
throttle_notch_mode = mode(throttle_notch); % * See comment below
throttle_notch_min = min(throttle_notch); % * See comment below
throttle_notch_max = max(throttle_notch); % * See comment below
T = table(no_avg) % * See comment below
end
*These variables are being overwritten on each iteration of the loop. You should either use them within the loop or store them properly in a vector or matrix.
댓글 수: 0
추가 답변 (1개)
Bob Thompson
2019년 6월 10일
Without direct access to your data I cannot give you a perfect solution to your data, so what I put here will likely take some finessing to get it to work perfectly with your dataset. From what I understand you're mostly having trouble with identifing and selecting the data with specific values, as opposed to calculating the mean, or other calculations you want to make. Therefore, I am going to focus on that part.
As I mentioned, I think a loop will be simplest here.
range = unique(t1{:,{'x0102ThrottleNotch'}}); % Identify different values, incase they change
for i = 1:length(range)
r = find(t1{:,{'x0102ThrottleNotch'}}==range(i)); % Return rows for corresponding throttle notch value
... % Do you other calculations for each throttle notch here
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!