I have a struct array a
:
a(1).s1 = NaN
a(2).s1 = 2
a(3).s1 = 4
a(4).s1 = 3
a(1).s2 = 1
a(2).s2 = NaN
a(3).s2 = NaN
a(4).s2 = 5
Ideally, I would like to create another struct array b
without entries where s2
contains NaN
. So the new array b
would look like this:
b(1).s1 = NaN
b(2).s1 = 3
b(1).s2 = 1
b(2).s2 = 5
I need this done automatically so I can apply the script to a much larger struct array.
I've been trying to create a for loop like this:
i = 1;
for i = find(all(~isnan([a(1:4).s2]), 1));
b(i).s1 = a(i).s1;
b(i).s2 = a(i).s2;
end
but it keeps empty entries.
Another attempt to remove entries with NaN
:
b = a;
i = 1;
for i = find(all(isnan([b(1:4).s2]), 1));
b(i) = [];
end
only works for the first NaN
. Then it loses i
count and removes incorrect entries.
Please help me out, if anyone knows how to do it.
You were really close to solving this but the for loop is a bit problematic so you should use array formula like this:
b = a(~isnan([a.s2]));
I hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.