Assignment of values in an empty matrix by logical indices

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

回答 (3 件)

David Sanchez
David Sanchez 2014 年 1 月 8 日

0 投票

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.
Jos (10584)
Jos (10584) 2014 年 1 月 8 日

0 投票

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
Stefan
Stefan 2014 年 1 月 8 日

0 投票

Thanks for fast reply. I still wondering that it is possible to assign values to a non existent element of matrix. However, I understand the issue and can handle it.
Regards Stefan

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2014 年 1 月 8 日

回答済み:

2014 年 1 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by