Filtering doubles in a struct by criteria of one double

15 views (last 30 days)
Hi all,
I'm newer to matlab and am trying to filter some data by a condition in one double of my struct. Here's my attempt:
id = site_data.wlH13 < 0.01;
site_data.wlH13(id) = NaN;
site_data.wlTmean(id) = NaN;
So the idea is that I'm creating a logical vector with the id of 1 and 0 which works correctly... and then the values of position '1' become NaN in the two doubles. But it does nothing and I don't understand why. My vectors are the same length so I don't think that could be the issue.
The desired out come would be filtering out values in wlH13 < 0.01 to be NaN and then the corresponding values in wlTmean would also be NaN.
I suppose I could write a loop but I'm doing this a lot with different parts of my data so I wanted to keep the loops to a minimum and was hoping to keep it vectorized.
I'd appreciate any help!
Cheers!
  3 Comments
Amy Bredes
Amy Bredes on 29 Dec 2021
So when looking at my data I realized the doubles from one of my intrusments is in one row and many columns not on column with many rows. In matlab does direction of data matter? This data is definitely not "tidy" in that orientation, does matlab have issues looping through data that way? Transposing data would be an easy solution if that's the case
Voss
Voss on 29 Dec 2021
Whether the variable is a column vector or a row vector should not matter for logical indexing (like you have here) or for looping over elements. Try transposing and see if anything is different, and/or, share (some of) the data here so we can try to reproduce the problem.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 29 Dec 2021
Edited: Star Strider on 29 Dec 2021
It seems to work correctly when I run it with random data —
% site_data.wlH13 = randn(1,15); % Vector
% site_data.wlTmean = randn(1,15) % Vector
%
% id = site_data.wlH13 < 0.01
% site_data.wlH13(id) = NaN;
% site_data.wlTmean(id) = NaN
I have no idea what the problem could be otherwise.
EDIT — (29 Dec 2021 at 18:37)
In matlab does direction of data matter?
No, not when dealilng with a logical matrix —
site_data.wlH13 = randn(10,5);
site_data.wlTmean = randn(10,5);
disp(site_data.wlH13) % Original Matrix
0.3003 0.9086 -1.2836 1.8215 1.7077 -0.9888 0.6339 0.2637 -1.3677 1.6837 0.9247 -0.9102 -0.2969 0.9729 -0.2158 -0.1942 0.9928 1.8947 1.2380 -0.3695 -0.3756 -0.3936 0.0046 -1.2277 0.0436 -0.1705 0.0382 1.3941 -0.4806 -0.8174 -0.6724 0.2722 -0.9517 1.0568 0.6946 -1.8165 0.3566 0.2843 0.0853 2.5592 -1.6213 -1.3947 1.0908 -0.6446 1.4818 0.2327 -0.3986 0.6838 0.7668 -0.1432
disp(site_data.wlTmean) % Original Matrix
0.6265 -1.4207 -0.2086 -1.4861 -1.9043 -0.7122 1.2252 1.8280 -0.5985 0.9328 1.5839 1.3330 -1.3305 1.3311 -0.7515 0.8710 0.5038 -1.1801 0.8015 1.1475 -0.0660 1.4313 -0.5130 -1.6096 0.1137 -1.3846 1.6282 -0.2993 1.9394 0.8071 1.1329 0.4906 0.8091 -0.8215 -1.2889 1.0612 -1.0081 1.6008 0.0384 1.3599 0.2117 0.3809 0.0677 0.9157 0.5923 2.3382 1.1527 -1.0338 1.3011 -0.4536
id = site_data.wlH13 < 0.01 % Logical Selection Matrix
id = 10×5 logical array
0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 0 0 1
site_data.wlH13(id) = NaN; % Substitute Value
site_data.wlTmean(id) = NaN; % Substitute Value
disp(site_data.wlH13) % Display Result
0.3003 0.9086 NaN 1.8215 1.7077 NaN 0.6339 0.2637 NaN 1.6837 0.9247 NaN NaN 0.9729 NaN NaN 0.9928 1.8947 1.2380 NaN NaN NaN NaN NaN 0.0436 NaN 0.0382 1.3941 NaN NaN NaN 0.2722 NaN 1.0568 0.6946 NaN 0.3566 0.2843 0.0853 2.5592 NaN NaN 1.0908 NaN 1.4818 0.2327 NaN 0.6838 0.7668 NaN
disp(site_data.wlTmean) % Display Result
0.6265 -1.4207 NaN -1.4861 -1.9043 NaN 1.2252 1.8280 NaN 0.9328 1.5839 NaN NaN 1.3311 NaN NaN 0.5038 -1.1801 0.8015 NaN NaN NaN NaN NaN 0.1137 NaN 1.6282 -0.2993 NaN NaN NaN 0.4906 NaN -0.8215 -1.2889 NaN -1.0081 1.6008 0.0384 1.3599 NaN NaN 0.0677 NaN 0.5923 2.3382 NaN -1.0338 1.3011 NaN
.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!