Coder Size Mismatch error

Hello All,
Here is the small function i want to convert to C using Matlab coder.
function []=fun()
x=ones(9,11).*[0:10:100];
var1 = 0;
for t=11:500
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
end
end
I am getting size mismatch error: [9,11] ~=[1,11] in Matlab coder.
Things I tried
function []=fun()
x=bsxfun(@times, ones(9,11),[0:10:100]); % Changed this line
var1 = 0;
for t=11:500
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1; % error in this line
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
if sum(x(:,t+1))/9<100-5
x(:,t+1)=x(:,t+1)+5;
end
end
end
Error : Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x.
function []=fun()
x=bsxfun(@times, ones(9,11),[0:10:100]);
x=repmat(x,[1,1290]) % Changed this line
var1 = 0;
for t=11:1290
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
if sum(x(:,t+1))/9<100-5 % error in this line
x(:,t+1)=x(:,t+1)+5;
end
end
end
Error: Sizes mismatch: [1290][9] ~= [14179][9]. in coder and also it is not correct because in my main file dimension of x and other variable is not matching. So I think it is better not to use repmat.
Please give your suggestions. I will try to apply in my code.
Thank You
Manoj

7 comentarios

dpb
dpb el 3 de Abr. de 2021
Certainly no idea what you're really trying to accomplish here -- just more-or-less randomly changing stuff without any information on the end result to be achieved makes it difficult at best and the crystal ball is back in the shop again for repair this morning.
I'll note that
x=repmat(x,[1,1290])
makes 1290 copies of the original x array in the 2nd dimension which results in
>> 1290*11
ans =
14190
>>
columns in x. This doesn't seem at all likely is what is/was intended given the rest of the code, but just what the idea is of the problem trying to be solved is simply unknown.
Here is my code. I haven't used coder till now, So randomly changing stuff based on my errors.
Pleas guide me what should I need to refer, or could you please explain me how should i overcome this index exeeds array dimensions. I know when matlab throws index exeeds array dimensions, I don't know about Matlab coder.
It would be great if you guys helps me with this.
function P_profile=randpower(dt,var1,var3)
p=[[3,179917] %var1 = var3 = 0
[10,183125]
[6,196000]
[21,196000]
[43,114933]
[969,92483]
[113,92483]
[43,59158]
[21,158547]
[6,158547]
[10,183125]
[3,158547]];
tmax=sum(p(:,1))+41;
coder.varsize('x'); % Since size of x is varying in for loop, so i should tell coder that it is variable size
x=ones(9,11).*[0:10:100]; % getting error in this line: [9x11]~=[1x11]
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
x(2,t+1)=x(2,t)+a2;
if x(2,t+1)<(100-var1) || x(2,t+1)>(100+var1)
x(2,t+1)=x(2,t);
end
x(3,t+1)=x(3,t)+a3;
if x(3,t+1)<(100-var1) || x(3,t+1)>(100+var1)
x(3,t+1)=x(3,t);
end
x(4,t+1)=x(4,t)+a4;
if x(4,t+1)<(100-var1) || x(4,t+1)>(100+var1)
x(4,t+1)=x(4,t);
end
x(5,t+1)=x(5,t)+a5;
if x(5,t+1)<(100-var1) || x(5,t+1)>(100+var1)
x(5,t+1)=x(5,t);
end
x(6,t+1)=x(6,t)+a6;
if x(6,t+1)<(100-var1) || x(6,t+1)>(100+var1)
x(6,t+1)=x(6,t);
end
x(7,t+1)=x(7,t)+a7;
if x(7,t+1)<(100-var1) || x(7,t+1)>(100+var1)
x(7,t+1)=x(7,t);
end
x(8,t+1)=x(8,t)+a8;
if x(8,t+1)<(100-var1) || x(8,t+1)>(100+var1)
x(8,t+1)=x(8,t);
end
x(9,t+1)=x(9,t)+a9;
if x(9,t+1)<(100-var1) || x(9,t+1)>(100+var1)
x(9,t+1)=x(9,t);
end
if sum(x(:,t+1))/9<100-5
x(:,t+1)=x(:,t+1)+5;
end
if sum(x(:,t+1))/9>100+5
x(:,t+1)=x(:,t+1)-5;
end
temp=x(:,t+1);
temp(temp<(100-var1))=(100-var1);
temp(temp>(100+var1))=(100+var1);
x(:,t+1)=temp;
end
x=x';
P_1s=[];%zeros(3,9);
% P_1s=zeros(0,9);
for i=1:size(p,1)
P_1s=[P_1s;ones(p(i,1),9)*p(i,2)];
end
P_1s=[ones(12,9)*p(1,2)/9;(P_1s+var3/100*P_1s.*randn(size(P_1s))/2)/9;zeros(30,9)].*x/100;
P_profile=[interp1([0:1:size(P_1s,1)-1],P_1s,[0:dt:size(P_1s,1)-1])];
Thank You.
Happy Sunday
dpb
dpb el 3 de Abr. de 2021
We have no idea what you're trying to do here...explain the problem trying to solve.
Can't write code without a problem specification that is lacking.
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju el 4 de Abr. de 2021
Editada: Naga Manoj Kumar Lakkoju el 4 de Abr. de 2021
Hi dpb,
I have to convert .m file to c/cpp and run generated c/cpp code in visual studio, then compare both results.
When I am converting .m file I am getting size miss match error with this line
x=ones(9,11).*[0:10:100];
Error :
I replaced x = ones(9,11).*[0:10:100]; with x=bsxfun(@times, ones(9,11),[0:10:100]);. even though both are same.
Now In for loop: Even Though I declared x as variable size Why coder is throwing error?
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1)
x(1,t+1)=x(1,t);
end
Error:
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?
My question is
1) 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"
Hope you understand my question.
Thank you,
dpb
dpb el 4 de Abr. de 2021
Editada: dpb el 4 de Abr. de 2021
C uses static memory allocation; MATLAB will (in circumstances) reallocate dynamically -- but the C compiler won't automagically insert malloc() and/or memcopy() instructions like MATLAB does transparently for you behind the scenes.
for t=11:500
a1=(rand-0.5)*1;
x(1,t+1)=x(1,t)+a1;
...
What's the point here and what's the intended final size for x and how is that to be determined?
Write the code to preallocate x to its final size first, then don't try to reference outside those array boundaries
Naga Manoj Kumar Lakkoju
Naga Manoj Kumar Lakkoju el 4 de Abr. de 2021
Editada: Naga Manoj Kumar Lakkoju el 4 de Abr. de 2021
Yeah, Got it. Thank for the questions . Here is updated code.
coder.varsize('x');
x=zeros(9,1300);
x=bsxfun(@times,ones(9,11),[0:10:100]);
x=[x, zeros(9,1279)];
% Not related to previous code.
I have one more doubt
Let say, I have a structure, and When I try to generate code in Matlabcoder it is showing error
If i apply same questions here, a is executed 10 times which means it will have 10 instances. So do I need to pre allocate all the 10 fields? If that is the case then it would be messy if we have more filelds or max loop is so high. there must some way to do this
dpb
dpb el 4 de Abr. de 2021
I'm not all that familiar with the coder, but look at/read the documentation thoroughly first...
<Coder controlling-memory-allocation> looks like a good starting point.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Abr. de 2021

Editada:

el 4 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by