Subscript indices must either be real positive integers or logicals only in the loop

1 vue (au cours des 30 derniers jours)
Dear all,
I'm stuck with the error "Subscript indices must either be real positive integers or logicals." in the code below. When I delete the loops and do manually for different values of i and ii, no error appears. Could you please help to understand what is wrong?
[~, m] = ismember(date30(:,1:5),date_sep2(:,1:5),'rows');
date30_all=NaN(length(date30),size((data),2)+6);
date30_all(:,1:5)=date30(:,1:5);
for i=2:size((date30),1);
date30_all(i,6:end)=out(m(i,1),6:end);
for ii=6:size((date_sep2),2);
if isnan(date30_all(i,ii));
date30_all(i,ii)=out(m(i-1,1),ii);
else
end
end
end

Réponse acceptée

Guillaume
Guillaume le 10 Mai 2016
When reporting an error always report the whole error message including the line at which it occurs.
A guess is that one of your date30 is not member of date_sep2 and therefore one of the m element is 0. As you're using m(i, 1) as an index into out this causes problem.
If that is the case, the proper to fix this depends on what you want to do when a date30 is not found.
As an aside, you're taking a risk using length in your code. length is going to return the number of columns if there are less rows than columns. This would be a lot more robust since it will work regardless of the numbers of rows in date30:
date30_all = NaN(size(date30, 1), size(data, 2)+6); %and no need for extra brackets around date
I would recommend never using length. Use size with an explicit dimension for matrices, and numel for vectors.
As an other aside, since m is a vector, I would write m(i) instead of m(i, 1). That way it does not matter if m is a row or column vector.
  1 commentaire
Ekaterina Serikova
Ekaterina Serikova le 10 Mai 2016
Thanks a lot! I've added an "if" condition to check whether m(i) is not 0, and the code has worked.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 10 Mai 2016
m has entries for all of the date30(:,1:5) values, not just the ones that match. m will be 0 for the ones that do not match. You are not testing your m before using them.
The first output of ismember would be a logical vector indicating whether the m was valid or not.
  1 commentaire
Ekaterina Serikova
Ekaterina Serikova le 10 Mai 2016
Thanks a lot! I've added an "if" condition to check whether m(i) is not 0, and the code has worked.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by