Writing to a new matrix after filtering outliers form original matrix

2 Ansichten (letzte 30 Tage)
Greg B. am 7 Mär. 2016
Kommentiert: Moe_2015 am 7 Mär. 2016
Greetings. I am reading in a matrix of ~10,000 values. I want to go through each element and determine if that value is in between a certain boundary limit. Given a for loop with a nested if-statement, how do I write to a new matrix, 'newdata', other than just printing out the last value in the original matrix, 'data'? My code is below:
clc;
clear all;
close all;
for i = 1:length(data)
x = data(i);
limits = 5;
if (x < limits) && (x > (limits*-1))
newdata = [x];
end
end
Thank you for the help.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Moe_2015 am 7 Mär. 2016
This should work. Basically run another index called "k" as a sum that only increases when the inequalities are satisfied.
clc;
clear all;
close all;
k=0;
for i = 1:length(data)
x = data(i);
limits = 5;
if (x < limits) && (x > (limits*-1))
k=k+1;
newdata(k) = x;
end
end
2 KommentareKeine anzeigenKeine ausblenden
Greg B. am 7 Mär. 2016
Thank you, sir! This solution works. I am now iterating through the data matrix and with the 'k' index, I am now writing all the values that meet the inequality to the new matrix, 'newdata'. Great!
Moe_2015 am 7 Mär. 2016
No problem! Glad I could help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing 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!

Translated by