Adding values to a table from an if statement
10 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 16 Jul 2021
X = (157.5:315:31342.5).';
Z = table();
maxnum = max(T.Var2);
minnum = min(T.Var2);
Z.Var1 = minnum < X & X < maxnum;
0 Comments
Cris LaPierre
on 16 Jul 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
Z(:,1) = V
1 Comment
Peter Perkins
on 27 Jul 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.
See Also
Categories
Find more on Graphics Object Programming 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!