Assignment of values in an empty matrix by logical indices
2 次查看(过去 30 天)
显示 更早的评论
I have the following behavior but I expected another one:
>> H=zeros(3,0)
H =
Empty matrix: 3-by-0
>> H(3,false)=1
H =
Empty matrix: 3-by-0
>> H(4,false)=1
H =
Empty matrix: 4-by-0
It seems the value is not assigned but the matrix dimension is extended. Maybe someone is able to explain this behavior.
Thanks Stefan
0 个评论
回答(3 个)
David Sanchez
2014-1-8
matlab start indexing arrays with 1. Arrays in Matlab do not have element in position 0.
H(3,false) = H(3,0)
what does not exist: you do not change anything with that line.
H(4,false) add an extra row to the original matrix but does not assign a value to its elements since H(4,0) is not an element.
0 个评论
Jos (10584)
2014-1-8
For a matrix M, and scalars R, C and V, the expression
M(r,c) = V
means that you want to set the value of the Rth row and Cth column of the matrix M to the value V. If that row or column does not exist, create it.
For a logical vector TF
M(r,TF) = V
means to set the elements of the Rth row and the columns where TF is true to the value V. Again, if row R does not exist, create it.
So, the following makes sense:
A = 2, A(2,3) = 4
B = zeros(2,0), B(3,[false true false]) = 99
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!