Adding values to a table from an if statement
조회 수: 8 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Walter Roberson
2021년 7월 16일
X = (157.5:315:31342.5).';
Z = table();
maxnum = max(T.Var2);
minnum = min(T.Var2);
Z.Var1 = minnum < X & X < maxnum;
댓글 수: 0
Cris LaPierre
2021년 7월 16일
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
Peter Perkins
2021년 7월 27일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!