Coder Size Mismatch error
Show older comments
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 Comments
dpb
on 3 Apr 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.
Naga Manoj Kumar Lakkoju
on 3 Apr 2021
dpb
on 3 Apr 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
on 4 Apr 2021
Edited: Naga Manoj Kumar Lakkoju
on 4 Apr 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
on 4 Apr 2021
Edited: Naga Manoj Kumar Lakkoju
on 4 Apr 2021
dpb
on 4 Apr 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.
Answers (0)
Categories
Find more on MATLAB Coder 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!
