Effacer les filtres
Effacer les filtres

Error subscript indices must either be real positive integers or logicals.

2 vues (au cours des 30 derniers jours)
Anantha Padmanabhan
Anantha Padmanabhan le 14 Sep 2016
for z=1:1:m
variable_sort(z).vlos=variable(z).vlos(filterfinal(z).filterindex);
end
So I have this code. the term variable is a 1*m structure with n fields and I want to filter it with the term filterindex which is in the structure with 1*m structure with a few fields. The code I tried to write is supposed to return back a new variable variable_sort by filtering the values in variable with the filterindex to return a 1*m structure with n fields(all fields have the same filterindex).
When i try to run this code, I get the error "Error subscript indices must either be real positive integers or logicals". I am not sure what I am doing wrong here. Help would be appreciated!
  2 commentaires
Adam
Adam le 14 Sep 2016
filterfinal(z).filterindex
is apparently not always a real positive integer or a logical. What are these values?
Anantha Padmanabhan
Anantha Padmanabhan le 14 Sep 2016
Modifié(e) : Anantha Padmanabhan le 14 Sep 2016
the values range between 1 to <18720. The length of variable(:).vlos is 18720 always. I just want to extract data based on the filter from the variable term.

Connectez-vous pour commenter.

Réponses (2)

Steven Lord
Steven Lord le 14 Sep 2016
For some value of z, the expression filterfinal(z).filterindex returns something other than a positive integer value or a logical. Set an error breakpoint and run your code, then look at the value of that expression when you stop at the breakpoint.

Walter Roberson
Walter Roberson le 14 Sep 2016
Remember that values might get rounded for display purposes, so a value might look like an integer but be just slightly non-integer.
fidx = filterfinal(z).filterindex;
bad_fidx = find(fidx < 1 | fidx ~= floor(fidx));
if ~isempty(bad_fidx)
error('bad index at offset %d, value %.99g\n', bad_fidx(1), fidx(bad_fidx(1)));
end
A common cause for these not-quite-integer indices is code along the lines of
t = 1 : 0.1 : 20;
fidx = t * 10;
Algebraically 0.1 is 1/10 and you would tend to think that the * 10 would result in a clean integer. However, binary floating point cannot exactly represent 0.1, and when you multiply by 10 again, the result is not always exactly an integer. In the above simple example, the actual values that are not exactly integers are:
24.000000000000003552713678800500929355621337890625
29.000000000000003552713678800500929355621337890625
43.00000000000000710542735760100185871124267578125
48.00000000000000710542735760100185871124267578125
51.00000000000000710542735760100185871124267578125
56.00000000000000710542735760100185871124267578125
58.00000000000000710542735760100185871124267578125
61.00000000000000710542735760100185871124267578125
63.00000000000000710542735760100185871124267578125
81.0000000000000142108547152020037174224853515625
86.0000000000000142108547152020037174224853515625
92.0000000000000142108547152020037174224853515625
97.0000000000000142108547152020037174224853515625
102.0000000000000142108547152020037174224853515625
107.9999999999999857891452847979962825775146484375
112.9999999999999857891452847979962825775146484375
117.9999999999999857891452847979962825775146484375
123.9999999999999857891452847979962825775146484375

Community Treasure Hunt

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

Start Hunting!

Translated by