Error in Matlab Coder: Index exceeds array dimensions - arrays

I am trying to convert .m script to C++ using MATLAB Coder.
function P=r_p(1,var1,var3)
p=[[3,7]
[10,15]
[6,19]
[21,19]
[43,11]
[969,2]
[113,9]
[43,59]
[21,15]
[6,15]
[10,18]
[3,15]];
tmax=sum(p(:,1))+41;
coder.varsize('x');
x=ones(9,11).*[0:10:100]; % getting error in this line: [9x11]~=[1x11]. Since size of x is varying in for loop, so i should tell coder that it is variable size, So I used Varsize
for t=11:tmax
a1=(rand-0.5)*1;
a9=(rand-0.5)*1.25;
a2=(rand-0.5)*1.5;
a8=(rand-0.5)*1.75;
a3=(rand-0.5)*2.0;
a7=(rand-0.5)*2.25;
a4=(rand-0.5)*2.5;
a6=(rand-0.5)*2.75;
a5=(rand-0.5)*3;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1) % loop 1: x(1,11)+a1 value is is writing to x(1,12) So coder gives error "Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x".
x(1,t+1)=x(1,t); % In matlab it works fine, but coder throws error.
end
end
My question is Let say loop 1,
x(1,12)= x(1,11)+a1 In matlab this assignment works fine, but when converting it is throwing error " Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x" As I declared x as variable size coder should assign x(1,11)+a1 value to x(1,12) but it is not doing, instead throwing error. Why?
Since t is looping for 1289, if I specify bounds for x like
coder.varsize('x',[1290,1290],[0,0]) then Coder gives error in other part of the code i.e dimensions doesn't match. Ofcourse it should because dimension of x doesn't match with [ones(12,9)p(1,2)/9;(P_1s+var3/100P_1s.*randn(size(P_1s))/2)/9;zeros(30,9)].
is declaring x as variable size is correct step or not? if yes then what should be the work around for "index exceeds array dimensions error"
Please Let me know, what am I missing to convert it to C++ code

MATLAB Coder doesn't support 2 things you're using here: implicit expansion and growing arrays by assigning past the end of a dimension.
For implicit expansion, you can use:
x=bsxfun(#times,ones(9,11),[0:10:100]);
Assigning past the end of an array in MATLAB will grow the array. That's an error in Coder. There are 2 ways to overcome this:
Allocate your array to have the right number of elements up front
Use concatenation to grow an array: x = [x, newColumn]
In this example, you know tmax so I'd suggest just changing the allocation of x to have the right number of columns up front:
% Current initial value
x=bsxfun(#times,ones(9,11),[0:10:100]);
% Extra columns - please check my upper bound value
x=[x, zeros(9,tmax)];

Related

Error when using CVX package with large sparse matrix

The error description is as follows:
Error using full request 68813x68813 (35.3GB) array to exceed the preset maximum array size Creating an array larger than this limit can take a long time and result in no response from MATLAB for more information, see Array Size Limits or Default Items panel.
Error schurmat_sblk (line 35)
if issparse(schur); schur = full(schur); end;
The function file schurmat_sblk is a file in cvx\sdpt3\Solver,
How can I do to avoid this error?
My cvx codes are as follows:
The value you may need are: n=8; d=2^n;m=(d^2)*0.05;the size of Pauli is m*d^2, it's a sparse matrix. The size of y is m*1;
function [rhoE] = test_compressed_cc(n,~,m,Pauli,y)
d = 2^n;
cvx_begin sdp quiet
% how to define the variable ?
variable rhoE(d,d) hermitian;
rhoE == hermitian_semidefinite(d);
% ||x||_tr=tr(sqrt(x^\daggerx))=tr(sqrt(x^2))=Tr(x)
minimize(trace(rhoE));
subject to
(d/m)*(Pauli * vec(rhoE)) == y;
rhoE >= 0;
cvx_end
end
On the other hand, maybe CVX can't solve the 8 qubit case, does anyone know how SVT should be used to solve this convex program.
Paper link: https://arxiv.org/pdf/0909.3304.pdf
Welcome any comment : )

Can't create a 'real' type array in Verilog

I've tried creating a 'real' type value array in the following way in Icarus Verilog:
parameter width = 10;
shortreal b [width-1:0] = {0.0181,0.0487,0.1227,0.1967,0.2273,0.1967,0.1227,0.0487,0.0181};
Gives the following error:
error: Cannot assign to array b. Did you forget a word index?
I went through the icarus verilog src code error messages and the explanation to this one is "Special case: The l-value is an entire memory, or array
slice". This is, in fact, an error in l-values. Detect the
situation by noting if the index count is less than the
array dimensions (unpacked)", which I assume means that the array index size differs from the declared one [width-1:0], which isn't true if I understand.
I've also tried:
parameter width = 10;
parameter [32:0] b [width-1:0] = {0.0181,0.0487,0.1227,0.1967,0.2273,0.1967,0.1227,0.0487,0.0181};
but without any success.
Using Icarus Verilog with -g2012 flag (for SV support)
It is only legal to use so-called array concatenation if you fill the entire array. You array have 10 elements, but there are only 9 on the right hand side:
parameter width = 10;
shortreal b [width-1:0] = {0.0181,0.0487,0.1227,0.1967,0.2273,0.1967,0.1227,0.0487,0.0181,0.0181};

How do I call a function handle from cell array?

What I try to do is keep a cell-array of function handles, and then call one of them in a loop. It doesn't work, and it seems to me that what I get is just a 1x1 cell array, and not the function handle within it.
I am not fixed on using cell arrays, so if another collection works it's fine by me.
This is my code:
func_array = {#(x) x, #(x) 2*x }
a = func_array(1) %%% a = #(x) x
a(2) %%% (error-red) Index exceeds matrix dimensions.
a(0.2) %%% (error-red) Subscript indices must either be real positive integers or
logicals.
Thank you
Amir
The problem is in this line:
a = func_array(1)
you need to access the content of the cell array, not the element.
a = func_array{1}
and everything works fine. The visual output in the command window looks the same, which is truly a little misleading, but have a look in the workspace to see the difference.
As mentioned by chappjc in the comments, the intermediate variable is not necessary. You could call your functions as follows:
func_array{2}(4) %// 2*4
ans = 8
Explanation of errors:
a(2) %%% (error-red) Index exceeds matrix dimensions.
a is still a cell array, but just with one element, therefore a(2) is out of range.
a(0.2) %%% (error red) Subscript indices must either be real positive ...
... and arrays can't be indexed with decimals. But that wasn't your intention anyway ;)

Changing Array size inside a loop causing Error in matlab

I am trying to dynamically set the size of array and store some value in it but it is causing an error.
here is the code,
syms k
x=[1 0 0 1];
y=[];
for b=1:4
step1= x(b)*exp(-2*pi*1i*k*((b-1)/length(x)));
y(b)=step1
end
what i am trying to do is to store each value of step1 in the array 'y'.
and here is the error,
The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in Untitled3 (line 6)
y(K)=1/exp((pi*k*3*1i)/2)
Depending on what you're trying to do, Matlab struggles to go from double to symbolic, so you should make it clear from the get-go that y is to contain symbolic elements:
syms k y
x=[1 0 0 1];
for K=1:4
step1= x(K)*exp(-2*pi*1i*k*((K-1)/length(x)));
y(K)=step1
end
Is there a reason why you are using a symbolic variable k and a loop counter K? It looks like you are confusing the two. I think this is what you are trying to implement:
x=[1 0 0 1];
y=[];
for k=1:4
y(k)= x(k)*exp(-2i*pi*k*((k-1)/length(x)));
end
Note: When working with large loops, it is much faster for MATLAB to pre-allocate the array rather than dynamically resizing it. For example by changing y=[]; to y=zeros(1,4);

The idea of C array in matlab

Is it possible to apply the idea of array in C to MATLAB
For example, if we have
Double array[10];
and if we want to assign a value we write for example
Array[5]=2;
Is there any way to write equivalent one in MATLAB ?
I'm not sure what you mean by "Is it possible to apply the idea of array in C to MATLAB". An array is just a 1D list of numbers (or other data types). MATLAB primarily is designed to work with matrices (MATLAB is short for Matrix laborartory) and an array or vector is simply a special case of a matrix. So I guess the answer to your question is yes, if I have understood correctly.
To initialise arrays or matrices in MATLAB we use zeros or ones:
>> array = zeros(1,5)
array =
0 0 0 0 0
We can then index elements of the array in the same way as C:
>> array(3) = 3
array =
0 0 3 0 0
Note, however, that MATLAB array indexing is one based whereas C arrays are zero based.
This article describes matrix/array indexing in MATLAB.
You can define your own class, override the [] operator.
I described the mechanism in Here
Since it is a custom function, you might as well change the 1-based indexing to 0-based indexing.
Regarding the constructor, I doubt that you can do it.
Anyhow, why would you want to do it?
You will confuse all of the Matlab users, and cause havoc.
When in Rome, do as Romans do.
Yes you can. Arrays are used in C and MATLAB and they can be used for the same functions. Except, please keep in mind the array-indexing of C and MATLAB are different.
The first element of a C array has an index of zero. i.e. in X = [10 20 30 40], x[0] will return 10. But in MATLAB, this will give an error. To access the number 10, you have to use the expression x[1] in MATLAB.
There is no indexing operator []. You must use () for indexing array.
If you write
x = 1:10;
x[2]
then you'll get the following error
x[2]
|
Error: Unbalanced or unexpected parenthesis or bracket.

Resources