Assignment of values in an empty matrix by logical indices
2 views (last 30 days)
Show older comments
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 Comments
Answers (3)
David Sanchez
on 8 Jan 2014
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 Comments
Jos (10584)
on 8 Jan 2014
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!