Is there a way to start indexing with 0 in MATLAB?

조회 수: 149 (최근 30일)
IBM watson
IBM watson 2019년 1월 2일
댓글: Rohit 2023년 7월 5일
a=[4 2 5 2]
can I make this happen?
a(0)=4

채택된 답변

madhan ravi
madhan ravi 2019년 1월 2일
Nope not at all index of matlab starts from 1 always!
  댓글 수: 10
Bruno Luong
Bruno Luong 2023년 7월 3일
You confuse between uint8 arithmetics (that can never return anything beyond (0:255))
and indexing range.
Rohit
Rohit 2023년 7월 5일
@Stephen23 @Bruno Luong Thank you for your inputs. I was under the assumption that typecasting the index would change the the data type of the output which is not the case.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Bruno Luong
Bruno Luong 2019년 1월 2일
편집: Bruno Luong 2019년 1월 2일
Yes, but you won't get much support by built-in array stock function
>> M = containers.Map('KeyType','uint32','ValueType','double')
M =
Map with properties:
Count: 0
KeyType: uint32
ValueType: double
>> M(0)=3
M =
Map with properties:
Count: 1
KeyType: uint32
ValueType: double
>> M(0)
ans =
3
>>
  댓글 수: 6
IBM watson
IBM watson 2019년 1월 3일
Many thanks!!
Utku Yilmaz
Utku Yilmaz 2020년 4월 19일
편집: Utku Yilmaz 2020년 4월 19일
ZeroBased function (link given above) is very useful. 1 based indexing creates problem when I share my code with software developers using C language. The indexes get messy and it becomes very hard to compare codes.
I always used index values starting from 0, as following. But I think, I will utilize ZeroBased function from now on.
for index_A = 0 : 10
for index_B = 0 : 10
value = a_matrix(index_A+1, index_B+1);
end
end

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2023년 7월 3일
One way is to define your onw class with overloading subsref and subsasg

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by