How to extract rows which contains specific values in each row, then automatically repeat the process for a range.

5 Ansichten (letzte 30 Tage)
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
  4 Kommentare
Bob Thompson
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?
Nasir
Nasir am 10 Jun. 2019
Bearbeitet: Nasir am 10 Jun. 2019
there is no error message but the code is not doing what i want it to do, for example if a column has 100 total values , with the values ranging from 0-8 (13/100 read 0 , then 20 that read 1 , and so on) , the code is taking all 100 values and taking an average of all of them , where as i want the code to take only the values that read 0 and take that average. as well as record the rows where the values read 0 in order to do calulations in other columns. so as you see above for No 0 i want no_avg to read 0 where as it is taking all the values 0-8 and averaging them. below No 0 - 8 show the same values.Capture.PNG

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
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.

Weitere Antworten (1)

Bob Thompson
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

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by