matlab generate c code with get column values error - matlab

i have meet a problem when using coder to generate C coder.
matlab function contains a sentences like
function [ B ] = fn1( A )
a = A(:,2);
B = a+1;
end
A is input parameter, 4x2 matrix.
i got c code:
void fn1(const float A[8], float B[4])
{
int i0;
for (i0 = 0; i0 < 4; i0++) {
B[i0] = A[4 + i0] + 1.0F;
}
}
B is not then 2nd column of A.
in matab "define input types", i changed row/column ,it still not working.
i'm using matlab 2016b. is there additional setting or advice to solve this problem?
thanks.

KEYWORD: array layout.
matlab array is column layout while C array is row layout. this mismatch causes the problem. matlab introduces 'array layout' option in R2019 or ealier, but it's not available in R2016.

Related

How to write functions as matrix elements inside MATLAB matrices?

I am interested in writing some matrix elements as functions which can take value as per my convenience and then the required matrix operations can be applied on top of that. More precisely, I am trying to integrate over x, the trace of a matrix which has matrix elements as functions of x (which are unknown analytically as they come through products of matrices dependent on x) .
When I try to write the matrix elements as functions, then I obviously get the error- Conversion to double from function_handle is not possible. Is there an easy way to write the matrix elements as functions ?
Thanks. Please ask if my question is not clear.
For example, its something like this:-
N_k = 10;
M_sigma = cell(N_k,1);
M_rho = cell(N_k,1);
for ii = 1:N_k
M_sigma {ii}(1,2) = #(sigma) sigma; %this kind of thing is not allowed in matlab
M_sigma {ii}(2,1) = #(sigma) -conj(sigma);
M_sigma {ii}(1,1) = 0;
M_sigma {ii}(2,2) = 0;
end
for ii = 1:N_k
M_rho {ii}(1,2) = #(rho) rho;
M_rho {ii}(2,1) = #(rho) -conj(rho);
M_rho {ii}(1,1) = 0;
M_rho {ii}(2,2) = 0;
end
M_tau = cell(N_k,1);
for ii = 1:N_k
M_tau {ii} = exp(M_sigma{ii})*exp(M_rho{ii});
end
% the following statement is wrong but I want to do something like
%:-write M_tau as a function of sigma and sum(integrate) the trace of M_tau for all values of sigma
integral(#(sigma) M_tau{1}(sigma), {0,1})
I don't think that you would be able to accomplish that with function handles. I think the best way you could do it would be to define a function as follows :
function x = myFunction(rowindex,colindex)
x = x * rowindex + colindex;
end
You would replace this function with your algorithm and then you could iterate over it doing the following:
for a=1:10
for b=1:10
x(a,b)=myFunction(a,b);
end
end

Attempted to access b(1,2); index out of bounds because size(b)=[3,1]

I have a uni assignment to create a very basic function that works the same as the backslash command in MATLAB. I'm not looking for the answer to the uni, my intentions are not to cheat, I'm just stuck on a problem and need guidance on how to get this to work.
So I think I'm close to the answer.
function x = matrix_solve(A,b)
invA=inv(A);
[m,n] = size(A); % m is num rows, n is num of columns
nb = size(b,1);
if m == n && nb == m
x = zeros(m,n);
for i = 1:m
for j = 1:n
x(1,j) = x(1,j)+ invA(i,1)*b(1,j);
end
end
else
error('Error')
end
Using a basic square matrix
b = [1;3;2]
A = [3,1,2;3,2,1;3,3,3]
I keep getting the error mentioned in the title.
I'm pretty familiar with other code but I'm very very new to Matlab.
The cause of the index out of bound error is you're trying to access an element that doesn't exist at this line
x(1,j) = x(1,j)+ invA(i,1)*b(1,j);
Instead it should be
x(1,j) = x(1,j)+ invA(i,1)*b(j,1);
I.e. just change b(1,j) to b(j,1). bis a column vector of size 3 (size(b)=[3,1]) and you are trying to access an element at row 1, column 2, but there is only 1 column.

Formulating a summation objective function to minimize for fmincon in MatLab

I have a summation objective function (non-linear portfolio optimization) which looks like:
minimize w(i)*w(j)*cv(i,j) for i = 1 to 10 and j = 1 to 10
w is the decision vector
cv is a known 10 by 10 matrix
I have the formulation done for the constraints (a separate .m file for the project constraints) and for the execution of the fmincon (a separate .m file for the lower/upper bounds, initial value, and calling fmincon with the arguments).
I just can't figure out how to do the objective function. I'm used to linear programming in GLPK rather than matlab so I'm not doing so good.
I'm currently got:
ObjectiveFunction.m
function f = obj(w)
cv = [all the constants are in here]
i = 1;
j = 1;
n = 10;
var = 0;
while i <= n
while j<=n
var = var + abs(w(i)*w(j)*cv(i, j));
j = j + 1;
end
i = i + 1;
end
f = var
...but this isn't working.
Any help would be appreciated! Thanks in advance :)
So this is from a class I took a few years ago, but it addresses a very similar problem to your own with respect to use of fminsearch to optimize some values. The problem is essentially that you have a t, y, and you want a continuous exponential function to represent t, y in terms of c1*t.*exp(c2*t). The textbook I lifted the values from is called Numerical Analysis by Timothy Sauer. Unfortunately, I don’t remember the exact problem or chapter, but it’s in there somewhere.
c1 and c2 are found recursively by fminsearch minimizing a residual y - ((c1) * t .* exp((c2) * t)). Try copying and running my code below to get a feel for things:
%% Main code
clear all;
t = [1,2,3,4,5,6,7,8];
y = [8,12.3,15.5,16.8,17.1,15.8,15.2,14];
lambda0=[1 -.5];
lambda=fminunc(#expdecayfun,lambda0, ...
optimset('LargeScale','off','Display','iter','TolX',1.e-6),t,y);
c1=lambda(1);
c2=lambda(2);
fprintf('Using the BFGS method through fminunc, c1 = %e\n',c1);
fprintf('and c2 = %e. Since these values match textbook values for\n', c2);
fprintf('c1 and c2, I will stop here.\n');
%% Index of functions:
% expdecayfun
function res=expdecayfun(lambda,t,y) c1=lambda(1);
c2=lambda(2);
r=y-((c1)*t.*exp((c2)*t));
res=norm(r);
Hope this helps!

Compute the convolution of two arrays in MATLAB

I am trying to generate an array from some starting values using this formula in MATLAB:
yt = a0 + ∑i=1p (ai ⋅ yt-i), t ≥ p
p is some small number compared to T (max t). I have been able to make this using two for cycles but it is really slow. Is there some easy way to do it?
First p values of y are provided and vector a (its length is p+1) is provided too...
This is what I have so far, but now when I tried it, it doesn't work 100% (I think it's because of indexing from 1 in MATLAB):
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end
EDIT: I solved it, I am just not used to Matlab indexing from 1...
This statement
if(p>=t)
looks odd inside a loop whose index expression is
for t = p+1:T+1
which seems to guarantee that t>p for the entire duration of the loop. Is that what you meant to write ?
EDIT in response to comment
Inside a loop indexed with this statement
for j = 2:p
how does the reference you make to a(j) ever call for a(0) ?
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end

how to Convert Matlab fft function to cvDft in opencv

I'm Very new to opencv.I need to convert the code from matlab to opencv.i have problem with use fft in matlab.i have a one dimensional matrix a.and i'm going apply fft in that as given below.
a = [0;0;0;0;0;0;0;0;0.09707;0.0998;0.1202;-0.1606;-0.0913;0.1523;0.1288];
c = abs(fft(a,15));
c >> 0.3463
0.1056
0.3608
0.5705
0.4232
0.2407
0.1486
0.1488
0.1488
0.1486
0.2407
0.4232
0.5705
0.3608
0.1056
C is my result,which i got from matlab.while i'm going to use cvDFT for this one the results are differnt.please help me with some example..my c code is given below...
CvMat* fftin = cvCreateMat(nn,1,CV_64FC2);
CvMat* fftout = cvCreateMat(nn,1,CV_64FC2);
cvZero(b); ////consider a hav the value of mat b is empty for imgin
cvMerge(a,b,NULL,NULL,fftin);
cvDFT(fftin,fftout,CV_DXT_FORWARD,0);
cvSplit(fftout,out_real,out_img,0,0);
for (int i = 0;i<out_real->rows;i++)
{
double val11= cvGetReal2D(out_real,i,0);
double val12= cvGetReal2D(out_img,i,0);
val11 = abs(val11);
val12 = abs(val12);
printf("DFT value is:%f %f\n",val11,val12);
cvSetReal2D(C_out,i,0,val11);
}
In your first example, you seem to be printing the magnitude of each complex value in the result. But in your second example you seem to be printing the absolute value of each component of the complex values (e.g. X and Y instead of the length or hypotenuse).