필터 지우기
필터 지우기

Assignment of values in an empty matrix by logical indices

조회 수: 4 (최근 30일)
Stefan
Stefan 2014년 1월 8일
답변: Stefan 2014년 1월 8일
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일
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일
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일
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by