Adding values to a table from an if statement

조회 수: 8 (최근 30일)
Emma Krampe
Emma Krampe 2021년 7월 16일
댓글: Peter Perkins 2021년 7월 27일
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

답변 (2개)

Walter Roberson
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;

Cris LaPierre
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
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
Peter Perkins
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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by