How to extract rows which contains specific values in each row, then automatically repeat the process for a range.
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nasir
am 10 Jun. 2019
Bearbeitet: Adam Danz
am 11 Jun. 2019
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)
![Capture.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223837/Capture.png)
4 Kommentare
Bob Thompson
am 10 Jun. 2019
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?
Akzeptierte Antwort
Adam Danz
am 10 Jun. 2019
Bearbeitet: Adam Danz
am 11 Jun. 2019
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 Kommentare
Weitere Antworten (1)
Bob Thompson
am 10 Jun. 2019
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 Kommentare
Siehe auch
Kategorien
Mehr zu Data Import from MATLAB 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!