![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196385/image.png)
Subscript indices must either be real positive
1 回表示 (過去 30 日間)
古いコメントを表示
Hi it is a common error, yes I saw the other Q/A here, but still could not figure it out. the matrix indices cannot be zero, but I have values in the matrix are fractions 0.1,0.2,0.3etc, is that the issue?
I want to extract the (x,y) coordinates of the points on the boundary of the rectangular geometry as illustrated in the attached and save them in a matrix to use them later in an equation for each point.
first I tried to extract them for each side. I used the following procedure but stopped after a few points
for r=0:0.1:2
mat(r*10+1,:)=[1 r]
end
the error was
Subscript indices must either be real positive integers or logicals
Thanks
0 件のコメント
採用された回答
KALYAN ACHARJYA
2018 年 9 月 24 日
編集済み: KALYAN ACHARJYA
2018 年 9 月 24 日
for r=0:0.1:2
mat(fix(r*10+1),:)=[1 r] % Considering only integer using *fix*
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196385/image.png)
0 件のコメント
その他の回答 (3 件)
Dennis
2018 年 9 月 24 日
The error is actually not that straight forward. Many floating point numbers can described 100% accurate in binary form. So your r might be not exactly 0.6. You could use round to prevent this (although this might not be the cleanest approach).
for r=0:0.1:2
mat(round(r,2)*10+1,:)=[1 r]
end
You could aswell just assign the r vector to a matrix:
r=0:0.1:2;
A=ones(length(r),2);
A(:,2)=r';
0 件のコメント
KSSV
2018 年 9 月 24 日
r = 0:0.1:2 ;
mat = zeros([],2) ;
for i = 1:length(r)
mat(i*10+1,:)=[1 r(i)] ;
end
0 件のコメント
Walter Roberson
2018 年 9 月 24 日
r=0:0.1:2;
r(find(r*10 ~= fix(r*10)))
You will find that the entry that looks like 0.3 multiplied by 10 does not give exactly 3
You will also find that the entry that looks like 0.3 is not exactly equal to what you would get if you entered 0.3 in text form.
In short, do not run calculations on values with fractions and expect the results to be exactly integers unless you use round or floor or ceil or fix. Sometimes you can get away with it, but if the fraction involved is not exactly 0.5 or 0.25 then you should assume there might be problems.
Better would be to loop over integer indices and convert to r values if needed
for k=1:21
r=(k-1)/10
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!