If Statement in a table
4 次查看(过去 30 天)
显示 更早的评论
I have the table K below and would like to use an if statement that replaces the zeros in the fifth column with 300, everytime that the row one below contains a "Ja".
My if statement looks like this:
if K(i,2:5)=="Ja"
K(i-1,6) = 300
end
This is the error I get: Index in position 1 is invalid. Array indices must be positive integers or logical values.
What do I need to change to access the correct data?
Thank you for your help!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/787000/image.png)
0 个评论
回答(1 个)
Cris LaPierre
2021-11-2
Have you defined i? If not, it is sqrt(-1).
There error means i is either a decimal or negative number.
A=1:3;
% Works
A(2)
% doesn't work
A(1.5)
3 个评论
Cris LaPierre
2021-11-3
When indexing a table, paretheses return a table while curly braces return an array. You need to use curly braces to extract the value from the table.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!