1

I have a struct, that's a <1x1 struct>, and I'm trying to edit a field in the struct based on the values. The field is called GeoDist_Actual and the struct is called GeoDist_str. The field GeoDist_Actual is a <262792x1 double>, and this is the code I was trying to use in order to get rid of the values that are greater than 1.609344e+05.

i =1;
for i=i:size(GeoDist_str.GeoDist_Actual)
    if GeoDist_str.GeoDist_Actual(i,1 > 1.609344e+05
    GeoDist_str.GeoDist_Acutal(i,1) = [];
    end
end

How would I append or alter this code in order to make it function like I'm aiming? I considered setting all the values to 0, but I'm going to have to go backwards from this in order to get back GPS values, doing a reverse-Vincenty(spherical) calculation, and I'd like to just completely get rid of the values that don't comply with the if condition.

If I can narrow down the question at all, let me know, and thank you for your help in advance!

Edit: I've noticed that when I changed out the section

GeoDist_str.GeoDist_Actual(i,1) = []; 

for

GeoDist_str.GeoDist_Actual(i,1) = 0;

It didn't actually solve anything, instead it didn't access the field "GeoDist_Actual" within the struct "GeoDist_str", it just created a mirror field with values of 0.

Suever
  • 64,497
  • 14
  • 82
  • 101
Pierson Sargent
  • 175
  • 2
  • 18

2 Answers2

2

Consider this example:

% a 10-by-1 vector
x = [1;2;3;4;5;6;7;8;9;10];

% remove entries where the value is less than five
x(x<5) = [];

This is called logical indexing, no need for loops.

Amro
  • 123,847
  • 25
  • 243
  • 454
2

Consider the following simple example:

A.a = 1:5;

A = 

    a: [1 2 3 4 5]

now delete all elements bigger 3;

A.a = A.a( ~(A.a > 3) );

A = 

    a: [1 2 3]

or alternatively:

A.a( A.a > 3 ) = []

For your case it's a little more bulky:

GeoDist_str.GeoDist_Actual = ...
GeoDist_str.GeoDist_Actual( ...
~(GeoDist_str.GeoDist_Actual > 1.609344e+05) )
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • 2
    +1 Beat me by a second... I think it's because you shortened variable names :-) – Luis Mendo Jan 14 '14 at 23:37
  • 1
    A little more readable: `GeoDist_str.GeoDist_Actual(GeoDist_str.GeoDist_Actual>1.609344e+05) = [];` – Luis Mendo Jan 14 '14 at 23:40
  • What if you wanted to use this same method on two columns of data and you had a double dependency? As in, I had a necessary range for the value in one column and a necessary range for the value in the second column. These columns are the same dimension: 262792x1 – Pierson Sargent Jan 15 '14 at 05:23
  • Double dependency is no problem, the question is, what happens if they don't have the same dimensions afterwards? I think I'd rather store every array in a separate struct branch or instead of deleting you could substitute the values with `NaN`. Be a little more clear with that. But there are no matrices with rows/columns of different lengths – Robert Seifert Jan 15 '14 at 09:26