On 1/3/2012 3:17 AM, Marcus wrote:
> Hey guys,
> I have the following problem in a Simulink (User defined function) :
> The purpose is to search an 256 by 256 array(rand_pt), starting at
> random coordinates (random_coo_x_double,random_coo_y_double). The
> program is supposed to stop if it finds a value 1.
coo_x=randi(128,1);
coo_y=randi(256,1);
x_temp=1;
y_temp=1;
flag=0;
i=1;
j=1;
limit_x=128;
limit_y=256;
while(flag==0)
for i=coo_x:limit_x
for j=coo_y:limit_y
if (rand_pt(i,j)==1)
x_temp=i;
y_temp=j;
flag=1;
break;
end%if
end%for
if flag, break, end
end%for
if flag, break, end
end%while
> In Matlab this is working perfectly. I believe the error message (Colon
> operands must be all the same type, or mixed with real scalar doubles.)
> tells me that
> "i" and "random_coo_x_double" and "limit_x" should be of the same type.
> So far I tried having them all as integers(uint8), which did not work
> because my array has the size of 256x256. Then I tried uint16 and double
> which all worked in Matlab but when I copied the code to the Simulink
> block I got the error above.
I don't "know nuthink" about Simulink so this is hypothesis...
i and j above will be double (assuming Simulink has same rules as
Matlab). The documentation doesn't say what the default 'classname' is
for randi(), but perhaps it is one of the integer classes; I don't know.
Presuming again you can set breakpoints in Simulink, use the debugger
and do a "whos" on the three variables at the point of the for and see
what they are; that should tell you what is going on.
I rewrote the above function shortening names drastically to aid in
reading the code...
A couple of comments...it would be best practice in Matlab to learn to
_not_ use 'i|j' for variables in loop indices, etc. despite the
propensity for same in other languages. Matlab defines them as the
imaginary i and j and overloading those can cause unexpected
difficulties down the road when you really sometime do want the complex
values.
I'd wonder why use looping here at all, though...why not simply select
the starting point then convert that to a linear index and then use
find() and ind2sub w/ the offset of the starting point?
Something like
ix=randi(128,1);
iy=randi(256,1);
idx_strt = sub2ind(size(x),ix,iy); % the initial offset into array
idx_fnd = find(x(idx_strt:end)==1,1,'first');
if ~isempty(idx_find)
[ix,iy]=ind2sub(size(x),idx_find+idx_strt-1);
else
ix=0; iy=0;
end
You can decide about the return value(s) or whatever, if the resulting
find() does return an empty result.
--