If-function in tables does not work
1 回表示 (過去 30 日間)
古いコメントを表示
Tobias Kleinert
2022 年 5 月 11 日
コメント済み: Tobias Kleinert
2022 年 5 月 12 日
Dear community,
I am trying to solve an apparently simple problem for days now (code below), but I just can't find a solution...
I created a table (Tab) that contains two columns with numbers (numb1 & numb2, in the original it also contains strings). I want to create a new file (answer) in the same format, which contains two columns with zeros, but a one in case of the number 5 (i.e., for numb1: 0,0,0,0,1,0,0 & for numb2: 0,0,1,0,0,0,0). I tried to solve this with an 'if' function, but I get an error message that the usage of '=' is not supported for tables. The original 'struct' format didn't work either, neither did it work for cells... Please help!!
numb1 = [1;2;3;4;5;6;7];
numb2 = [7;6;5;4;3;2;1];
Tab = table(numb1, numb2)
for TabLength = 1:height(Tab)
if Tab(TabLength,numb1) == 5
answer(TabLength,1) = 0
else
answer(TabLength,1) = 1
end
end
xxx
0 件のコメント
採用された回答
その他の回答 (1 件)
Blue
2022 年 5 月 11 日
If I understand correctly, the cyclist has answered this question: https://www.mathworks.com/matlabcentral/answers/321819-how-to-find-index-of-a-value-in-cell-array
numb1 = {1;2;3;'A';5;6;7};
numb2 = {7;'D';5;4;3;2;1};
Tab = table(numb1, numb2)
ans1 = double(cellfun(@(x)isequal(x, 5), Tab.numb1));
ans2 = double(cellfun(@(x)isequal(x, 5), Tab.numb2));
answer = table(ans1, ans2)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Hypothesis Tests についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!