I've been working on creating c code with Matlab-coder. I have a function called melfunction that gives an error for line 20 of the code below.
function [ c ] = melfunction( x )
bank=melbankm(24,256,8000,0,0.4,'t');
bank=full(bank); %full() convert sparse matrix to full matrix
bank=bank/max(bank(:));
w=1+6*sin(pi*[1:12]./12);
w=w/max(w);
xx=double(x);
xx=filter([1-0.9375],1,xx);
xx=enframe(xx,256,80)
p = zeros(1,256); < --------------------- SOLUTION CHANGE TO p = zeros(256)
m = zeros(1,12); < --------------------- SOLUTION CHANGE TO p = zeros(12)
for i=1:size(xx,1)
y=xx(i,:);
s=y'.*hamming(256);
t=abs(fft(s));
t=t.^2;
p(i,:) = t; < --------------------------- ERROR HERE
c1=dctcoef*log(bank*t(1:129));
c2=c1.*w';
m(i,:)=c2;
end
error message looks like this
Error using melfunction (line 20)
Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] of array p.
Error in TESTINPUTS (line 2)
d0=melfunction(x)
If I come up with an answer I will post it when I find one.
Ok so I messed with it a bit and found that I believe I did not know how to correctly initialize p and m . In an attempt to add these lines of code for another part of Matlab-Coder I initialized the p and m variables incorrectly. This is the original initialization.
p = zeros(1,256);
m = zeros(1,12);
this is what I changed it too.
p = zeros(256);
m = zeros(12);
Related
I am super new to sml. I am trying to write a simple code that takes an array of 5 positions with certain numbers and returns the length of the smallest subarray that contains all numbers. However I am getting many error messages that I cannot find in Google. Can anyone help me? The code is the following
fun Min x y = if x>y then return y else return x
local
val a = Array.array (3,0)
val cordela = Array.array(5,0)
val k=0
val front=0
val tail=0
val min=5
update(cordela,0,1)
update(cordela,1,3)
update(cordela,2,3)
update(cordela,3,2)
update(cordela,4,1)
in
fun loop front =
case k>3 of
if sub(a,sub(cordela,front)-1) = 0 then k=k+1 else()
update(a,sub(cordela,front)-1),sub(a,sub(cordela,front)-1)+1)
front = front +1
|
min= Min (front-tail) min
if sub(a,sub(cordela,front)-1) = 0 then k=k-1 else()
update(a,sub(cordela,front)-1),sub(a,sub(cordela,front)-1)-1)
tail=tail+1
if 5>front then loop front+1 else min
end
The error messages that I get are:
pl2.sml:16.13-16.15 Error: syntax error: replacing OF with LBRACKET
pl2.sml:18.36 Error: syntax error: inserting LPAREN
pl2.sml:20.4 Error: syntax error: replacing BAR with EQUALOP
pl2.sml:22.5 Error: syntax error: inserting LPAREN
pl2.sml:26.4 Error: syntax error: inserting LPAREN
pl2.sml:27.2 Error: syntax error found at END
Edit: I am trying to write this code in sml. It is written in c++
while(front < N){
if( k < K ){
if ( e[cordela[front]-1] == 0 ) k += 1;
e[cordela[front]-1] +=1;
front++ ;
}
else{
min = MIN(front - tail ,min);
if ( e[cordela[tail]-1] ==1 ) k -= 1;
e[cordela[tail]-1] -= 1;
tail++;
}
}
As John Coleman says, SML/NJ will not give very helpful error messages. You could try and install Moscow ML instead, since it gives better error messages. Unfortunately, there are some things wrong with this code at a syntactic level that makes it difficult for the compiler to give a meaningful error. Here are some hints to get the syntax right so that you can focus on the algorithm problems:
Don't use local, use let.
Match each ( with a ); you have too many )s.
Declare fun loop ... = ... inside let and in.
Once you've done that, a template for the function that solves your problem could look like:
fun smallest_subarray (needles : Array.array, haystack : Array.array) =
let
val ... = ...
fun loop ... = ...
in
if Array.length needles > Array.length haystack
then ...
else loop ...
end
What if there's no solution to the problem, what will the function return? ~1? NONE?
If you're trying to convert a C++ program to SML, try and include the function part in such a way that it's obvious what identifiers are arguments to the function, and try to name them logically; I have no idea what cordela, e and k are, or if N is a function of the size of the input array, or a constant.
Since an idiomatic solution in SML uses recursion (the function calling itself) rather than iteration (while), you are dealing with both a non-trivial algorithm problem and another paradigm. Try instead to solve a similar, but simpler problem where the algorithm is more trivial and apply the recursion paradigm.
For example, try and write a function that finds the position of an element in a sorted array using binary search:
fun find x arr =
let
fun loop ... = ...
in
loop ...
end
The loop function would take the search bounds (e.g. i and j) as argument and return either SOME i if x is found at position i, or NONE. You could extend this problem in the direction of your original problem by then trying to write a function that determines if an input array, needles, occurs in another input array, haystack, in the order given in needles. You could first assume that needles and haystack are sorted, and then assume that they're not.
I am getting the above mentioned error while going through multiple loops. I really don't know how to explain the problem, But i will try my best
code:
function this = tempaddfilt(this,varargin)
fc = linspace(1,200,(200/0.5));
main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{});
for a = 1:length(fc) % fc
q = 0;
w = 0
for i = 1:length(this.segments) % total number signal
for k = 1:length(this.segments{i}) % total number of segments
filt_sig = eval(this.segments{i}(k).signal,this.segments{i}(k).signal(1)); % apply filter to the ith singal and kth segemnt
filt_sig = filt_sig';
main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref); % calculate the standard divitation of the filtered signal and previously calculated signal.
q = q+main{i}(k).seg_err(a); add all the error of the segments for the same FC
end
main{i}(1).sig_err(a) = q; % assign the sum of all error of the all segemnts of the same signal
w = w+main{i}(1).sig_err(a); % add all the error of the signals
end
main.filt_err = w; % assign the sum of all error of the all signals
end
this.error_norm = [this.error_norm ;main];
end
end
Basically I have 3 loops, 1st loop is for fc, 2nd loop is for signal and 3rd loop is for segemnts of the singal. Program works fine when fc = 1.
But when fc is 2, i get the following error:
Cell contents assignment to a non-cell array object.
in the line:
main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref);
that is when i =1 , k=1 ,a = 2
The problem seems to be with the way how you want to access the members of the main struct dynamically. You declared main as a struct, with
main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{});
BUT struct members can not be accessed by using braces {}. Here is a reference to a previous discussion related to dynamic indexing of struct arrays. So, basically, the problem is with "main{i}", which is not a valid way of dynamically indexing members of a struct.
Try following.
Change the struct declaration to explanation
main = struct('seg_err',[],'sig_err',[],'filt_err',[],'fc',[]);
Then, extract the field names by
FieldNames = fieldnames(main);
Then, you can reference the struct members like in
for
loopIndex = 1:numel(FieldNames)
main.(FieldNames{loopIndex})(1).seg_err(1) = 1;
end
find() function returns the indices where the elements are non-zero. I tried with different array sizes but both give error :
In an assignment A(I) = B, the number of elements in B and I must be the same.
I am confused because when the array size is same, still I am getting this error.
This is just to understand what went wrong:
LEt,
Example 1: Same array size
A = [20;21;3;45;5;19;1;8;2;1];
B = A;
for i =1:length(B)
pos(i) = find(A == B(i));
end
I should have got pos = [1,2,3,4,5,6,7,8,9,10]. But the loops exits after i = 7, giving `pos = [1,2,3,4,5,6]'
Example 2: Dissimilar array size
C = [20;1;10;3];
for i =1:length(C)
pos(i) = find(A == C(i));
end
Can somebody please explain what is wrong in my understanding and an illustration of how I can work with same and different array length of A and B? Thank you.
The problem is that find(A == 1) returns two indexes, both 7 and 10, and that can't be stored in pos(i), since pos(i) can only hold a single number.
Unfortunately, the generic error message happened to have the same name for the matrices as two of your matrices, which can be confusing before you'we seen it a few times.
I've tried to find the answer to this simple for loop question but I can't find any tutorials on it. I'm trying to calculate a specific entry of a column vector but the error "Error in MuPAD command: DOUBLE cannot convert the input expression into a double array" keeps showing up. I'm pretty sure that both entries in the for loop should come up as scalar functions of u1,...,u20 but I must be doing something wrong. My script reads as follows:
U = sym('u', [21 1]);
P(1,1) = 1;
Q(1,1) = 0;
for k = 1:20
P(k+1,1) = 1/20*(-.3*P(k,1) +.65*Q(k,1)+U(k,1))+P(k,1);
Q(k+1,1) = 1/20*(-.65*Q(k,1) + .3*P(k,1)-U(k,1))+Q(k,1);
end
P(21,1)
Q(21, 1)
Any help would be greatly appreciated.
As David suggests, maybe:
U = sym('u', [21 1]);
syms P Q
for k = 1:20
P(k+1,1) = 1/20*(-.3*P(k,1) +.65*Q(k,1)+U(k,1))+P(k,1);
Q(k+1,1) = 1/20*(-.65*Q(k,1) + .3*P(k,1)-U(k,1))+Q(k,1);
end
P(21,1)
Q(21, 1)
Saw a similar explanation elsewhere on stack overflow. 2 variables in an array
This question already has an answer here:
sum(Array) says index exceeds matrix dimensions [duplicate]
(1 answer)
Closed 3 years ago.
I am trying to find the minimum value of a function of two variables, and then find the value of the variables.
My method is to iterate the function through several values of the variables and then use the min function to find the lowest value.
minval = -10;
maxval = 10;
n = 1;
for x = minval:maxval
for y = minval:maxval
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
n=n+1;
end
end
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
fmin = min(f)
The problem is with the last line:
fmin = min(f)
I am getting the error
??? Index exceeds matrix dimensions.
Error in ==> Lab2 at 65
fmin = min(f)
Why is this? Any help is greatly appreciated.
Don't define a variable named min. Try this:
which min
What does it tell you?
Note that functions in MATLAB can be overloaded by creating variables with the same name. When you do so, you prevent the function from being accessed by MATLAB. This is RARELY a good idea, so don't do it. The solution is
clear min
So you will delete that variable you have created. Of course, if there was something important in that variable, stuff it somewhere else first.
It does indeed look like you have declared a variable called min and so Matlab now treats it like a variable and not a function so it thinks you are trying to index the variable min with the vector f.
But just a comment on your code, leaving off whatever f(442) is you could achieve the same thing in a much more matlabesque fashion without loops like this:
minval = -10;
maxval = 10;
X = minval:maxval;
Y = X;
[xx, yy] = meshgrid(X, Y);
F = abs(xx-1) + abs(yy-1) + abs(xx-3) + abs(yy-5) +abs(xx-8) + abs(yy-3);
Your f is now equivalent to F(:)' (without the final value...), prove it to yourself like this: sum(f(1:end-1) == F(:)')
F as a matrix probably makes more sense than f as a flat vector anyway and you can find the min of F like this: min(F(:))
This code runs perfectly when I plug it into my version of Matlab.
If the error is occuring on line 65, then there must be something else going on in your program. Try and turn this part of your program into a function, so that it won't be impacted by everything else that you're working on.