Adding values to a table from an if statement

7 vues (au cours des 30 derniers jours)
Emma Krampe
Emma Krampe le 16 Juil 2021
Commenté : Peter Perkins le 27 Juil 2021
I'm performing a sequential Gaussian analysis and am trying to populate a table using an if statement. I keep getting an error saying that dot indexing is not supported for varialbes of this type for this section of code. Basically, I have preset a table with zeros, and then the code checks against another table X if the value is smaller or larger than the min and max defined. I want the code to assess the if statement and then populate the table Z with 0 or 1 in the column specified. Thanks!
X = [157.5:315:31342.5];
Z = zeros(100,11);
maxnum = max(T.Var2);
minnum = min(T.Var2);
for a = 1:100
if X{a,1} > minnum & X{a,1} < maxnum
Z.Var1{a} = 1;
else
Z.Var1{a} = 0;
end
end

Réponses (2)

Walter Roberson
Walter Roberson le 16 Juil 2021
X = (157.5:315:31342.5).';
Z = table();
maxnum = max(T.Var2);
minnum = min(T.Var2);
Z.Var1 = minnum < X & X < maxnum;

Cris LaPierre
Cris LaPierre le 16 Juil 2021
Z is not a table. It is a 100x11 matrix of zeros. Therefore, you would use normal indexing (row, column) to assign values to it.
Also, you do not need the for loop. Create a logical array instead, and assign that to the desired column (see ch 12 of MATLAB Onramp).
I've made your matrix smaller for demontration purposes.
X = 1:10;
Z = zeros(10,2);
maxnum = 8;
minnum = 5;
V = X>minnum & X<maxnum
V = 1×10 logical array
0 0 0 0 0 1 1 0 0 0
Z(:,1) = V
Z = 10×2
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
  1 commentaire
Peter Perkins
Peter Perkins le 27 Juil 2021
Also, X is not a cell array, so X{a,1} wasn't going to work, you needed X(a,1). In any case, Wlater's non-loop sol'n is the way to go.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables 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