Trying to go from 1 matrix to a structure with a condition in the next column

3 vues (au cours des 30 derniers jours)

Hello, so I have a [1x996] matrix that I am trying to break up to read two different outcomes: for all values less than 1.5, the values are considered critical for all values greater than 1.5, the values are considered not critical Ideally, I want to make a struct with the first column stating the values in the 1x996 matrix and the second column stating if the number from the first column is critical or not critical.

I have tried a variety of for loops and if else statements, but I can't get the values to list them all together in the correct order (if I pull out all of the criticals and all of the not criticals, then they will not be in the right line position for matlab for my other files of code. The picture illustrates what I am trying to do.

This is where I am at:

if true
  % for i=1:length(LabeledSituations)
  All_CutIn = (strcmp(LabeledSituations(i).CutIn_Label,'CutIn'));
  CutIn_files = find(All_CutIn == 1);
  dx = LabeledSituations(i).Data.Signals(114).Value;
  dvx = LabeledSituations(i).Data.Signals(116).Value;
  TTC = dx./dvx; 
  real_TTC = TTC(CutIn_files)  
%%% i need to make a structure where the first column is the numerical
%%% values and the second column is the condition (critical or nc) 
   critical_TTC = real_TTC(real_TTC < 1.5);
   nc_TTC = real_TTC(real_TTC > 1.5);
end
------------------------------------------------------------

Réponse acceptée

Matt J
Matt J le 23 Mai 2017
Modifié(e) : Matt J le 23 Mai 2017
I want to make a struct with the first column stating the values in the 1x996 matrix and the second column stating if the number from the first column is critical or not critical.
I can't tell what you mean by "struct". It sounds like what you really want is a 2x966 matrix,
result=[A(:), A(:)<1.5];
  2 commentaires
MIRANDA WHAH
MIRANDA WHAH le 24 Mai 2017
this actually works perfectly! now I have a table of two columns, the first being the values and the second being the condition. however instead of the second column reading zero and ones, is there a way for me to change it to read 'not critical' or 'critical'? i can#t figure out how to do this in a matrix.
also, a struct is just structure. it is similar to a table but allows to link to more sets of data
Matt J
Matt J le 24 Mai 2017
Modifié(e) : Matt J le 24 Mai 2017
Not sure why Andrei's approach didn't work, but here's an analogous one with cell arrays
A=0:5; %example
t=A(:)<1.5;
s={'critical';'non-critical'};
result=[num2cell(A(:)), s(t+1)]
will give
result =
6×2 cell array
[0] 'non-critical'
[1] 'non-critical'
[2] 'critical'
[3] 'critical'
[4] 'critical'
[5] 'critical'

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 23 Mai 2017
t = real_TTC >= 1.5;
s = {'critical';'non_critical'};
T = table(real_TTC,s(t + 1),'VariableNames',{'A' 'critical'});
  3 commentaires
Guillaume
Guillaume le 24 Mai 2017
You would get an error if real_TTC is a row vector. To fix:
T = table(real_TTC(:), s(t + 1), 'VariableNames', {'A' 'critical'});
The t+1 is to convert logicals (0/1) into valid matrix indices (1/2).
I would recommend you use a categorical array instead of acell array of chars for your second column:
T = table(real_TTC(:), ...
categorical(real_TTC(:), [false, true], {'critical', 'non_critical'}), ...
'VariableNames', {'agoodname', 'criticality'})
Andrei Bobrov
Andrei Bobrov le 24 Mai 2017
Modifié(e) : Andrei Bobrov le 24 Mai 2017
Small typo. I'm fixed:
T = table(real_TTC(:), ...
categorical(real_TTC(:) >= 1.5, [false, true], {'critical', 'non_critical'}), ...
'VariableNames', {'agoodname', 'criticality'})
Thank you Guillaume!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by