how to remove from all fields in the same position of a struct?

3 views (last 30 days)
I am sorry for opening a new question, but i cant understand the other solutions here. i have a struct named training_set of size 1x15 with two fields. one is called model and the other samples. samples is 1x3003 cell for each model. Each of these cells is a matrix i.e x. I want to do the following. for each model, i check one by one the 3003 X.( 15x3003 checkings ). I start from training_set(1).samples. if for some of the 3003 X's, abs(det(X'X))<1e-10 then i want to replace these cells with empty cell, but for all the 15 samples. Then for training_set(2) and replace with empty in all other sets. In other words, if training_set(1).samples{5}=[] then so should training_samples(2){5},.., training_set(15).samples{5}. I ve tried doing this with the following code, but it returns the whole training_set.samples to by empty ( i know that the almost the half should remain). Does anyone know why? I am sorry for the long question, i hope i gave you the point of what i am trying. Thank you guys.
for i=1:1:15
for j = 1:1:3003
x = training_set(i).samples{j};
if abs(det(x'*x))<1e-10
for k=1:1:15

Answers (1)

Jonathan Epperl
Jonathan Epperl on 1 Feb 2013
What you did should work, my only idea about a possible error: are your samples "fat" or "tall"? If they are fat, i.e. n columns, m rows, n>> m, then x'*x results in a ( n x n) matrix, which will have maximally rank m, thus be singular and have zero determinant. Long story short: det(x'*x) ~= det(x*x').
Also, this is a little more compact than what you are doing, and maybe it helps you understand other solutions:
% some sample data
training_set(1).samples = {1,2,eps};
training_set(2).samples = {1,eps,3};
training_set(3).samples = {1,1,3};
i0 = cellfun(@(c) det(c.'*c)<1e-10, cat(1,training_set(:).samples) )
for i=1:numel(training_set)
training_set(i).samples(any(i0,1)) = ...
% ^^ Curly braces here if you want the cell element to be set to []. If you want to remove the cell element, then no curly braces
  1 Comment
Christos on 2 Feb 2013
Thank you Jonathan Epperl. All samples are of size n-by-m with m<=n. I ll see if i can do anything.

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!