Array Indicates msut be integer

2 views (last 30 days)
MOHAMMED BELAL
MOHAMMED BELAL on 2 Jun 2019
Commented: Guillaume on 2 Jun 2019
i have this code and run in matlab shows me thus error
(Array indices must be positive integers or logical values).
Please is there aby one can help
Error in Test (line 39)
D =[];
for i=1:size(SF,1)
d = sum(abs(SF(i)-VF));
D = [D d];
end
%% Finding Shortest Distance
SM = inf;
End = 1;
for i=1:length(D)
if(D<(i)<SM)
SM=D(i);
End=i;
end
end
  5 Comments
MOHAMMED BELAL
MOHAMMED BELAL on 2 Jun 2019
Edited: MOHAMMED BELAL on 2 Jun 2019
i modified it to if((d<i) && (i<SM)) but still the same error
and the second problem its fft() i used it for extract voice features but seem it dosn't work casue it gives me empty array as result when i call the function and pass the recording object please can you help
this is code what is wrong with it
function [pitech] = VoiceFeatures(RecordedData)
furier = fft(RecordedData(1,:));
plot(real(furier));
maxFreq = max(real(RecordedData));
pitech = find(real(furier)==maxFreq,1);
and calling function
VF = VoiceFeatures(RecordedData);
but gives me VS
ans =
1×0 empty double row vector
thank you again
Guillaume
Guillaume on 2 Jun 2019
It's difficult to help you if you make crucial typos in your question. You say you call your function with VF = ... and then tell us that VS is empty. Are VS and VF supposed to be the same variable? If not, what is the relationship between the two.
We've now moved to a completely different bit of code. I'm not sure how it relates to your original question and it's not clear if your original problem has been resolved (if not, see my answer).
This new bit of code doesn't make much more sense than your original one. Your function takes a signal (RecordedData), perform a fft on it, then try to find a value of the fft that is equal to the maximum value of the fft. Your
maxFreq = max(real(RecordedData));
is very misleading. Typically a signal is an amplitude not a frequency, so the maxfreq variable should probably be called maxAmplitude and typically a signal is purely real, so it's not clear what the intent of real is. Additionally, a fft doesn't return frequencies, it returns amplitude at fixed frequency. I'm not sure what you're trying to do, but I don't think your function does it at all.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 2 Jun 2019
I would strongly recommend you do not use End as a variable name. It's too close to the keyword end. Use a more meanigful name anyway.
The problem with your code is simple. You set End to -1 before your loop. Inside the loop if a condition is true you'll then set End to a valid index. But if that condition is never true, you'll never change End and it will still be -1 when the loop finishes. Of course, -1 is not a valid index. So first thing is to add a check after the loop and react appropriately. At the very least:
EndIndex = -1; %use a better variable name than End
for i = 1:numel(D) %prefer numel over length
if %some check
%...
End = -1;
end
end
assert(End > 0, 'if statement was never true. Code is not designed to cope with this case');
detected_class = UID(End);
The above issue is compounded by problems with your if test. I'm not sure which of the two versions you've posted is the one you're actually using. I'm assuming it's the first one because the second has many problems (e.g. mix up of d and D). Assuming your if test is:
if D<i<SM
There are 2 problems. First a<b<c doesn't do what you think. It compares a to b and results in either true (1) or false(0). It then compares that 0 or 1 to SM. As Star already told you, the proper way to write the comparison you mean is
if D<i & i<SM
The second issue is that D is a vector so D<i returns a vector of true and false values, not a scalar logical. The if statement will only be true if all elements of the vector are true. I would suspect that's never the case and hence why End stays at -1.
I'm actually not sure what you're trying to do here, so can't tell you how to fix that. It's very strange that you're comparing an index into a vector to the actual values of that vector.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!