matlab code error - Subscript indices must either be real positive integers or logicals.*

1 回表示 (過去 30 日間)
John
John 2013 年 1 月 14 日
Hi,
Would anybody know why I am getting the following error after the code below executes a couple of times (about 20 times).
??? Subscript indices must either be real positive integers or logicals.
I've had no problems with it up until today.
Thank you
for j=1:5000
transC = [zeros(size(T,1),1), cumsum(T,2)];
n = 10000;
states = zeros(1,n); %storage of states
states(1) = 8; %start at state 1 (or whatever)
rr = rand(1,n);
for ii = 2:n
[~,states(ii)] = histc(rr(ii),transC(states(ii-1),:));
end
states=states';
states = convert(states, 2);
baseFileName = sprintf('%d.xls', j);
fullExcelFileName = fullfile('C:\raw bin 2 data\', baseFileName); % yourFolder can be pwd if you want.
xlswrite(fullExcelFileName ,[states],'Sheet1', 'A1');
end

回答 (2 件)

per isakson
per isakson 2013 年 1 月 14 日
編集済み: per isakson 2013 年 1 月 14 日
The debugger will tell you. Put the code in a function. Execute
dbstop if error
in the command window. Run your function. Execution will stop just before the error is thrown. Inspect the values of the variables involved.

Image Analyst
Image Analyst 2013 年 1 月 14 日
You initialize states to all zeros. When it comes time to use it, inside transC, states(ii-1), which is states(1) when ii = 2, is still zero, so you are trying to get transC(0), and that causes an error.
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 1 月 14 日
No, John has states(1) = 8
John
John 2013 年 1 月 14 日
Hi,
I got it working again by restarting matlab. If it happens again I will use the debugger.
Thanks for the comments.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by