Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


dpb
Posts:
9,242
Registered:
6/7/07


Re: correct syntax for matlab..
Posted:
May 14, 2013 10:11 AM


On 5/14/2013 2:32 AM, virat rehani wrote: > dpb <none@non.net> wrote in message <kmo69j$7nn$1@speranza.aioe.org>... ...
>> >> x=rand(5); >> >> x(x>.6)=1;x(x<1)=0; % make up a set of data ...
>> Your solution... >> >> k=1;for i=1:5,for j=1:5,if x(i,j)==1,e(k,:)=[i j];k=k+1;end,end,end >> >> The "Matlab way"...(or one of the ways, anyway)... >> >> [e1(:,1) e1(:,2)]=find(x==1); >> >> e1=sortrows(e1); >> >> >> all(all(e==e1)) >> ans = >> 1 >> >> >> >> Shows oneforone consonance of each element in e and e1...
...
> Dear sir I did it this way counter=1; > eligible=[10,2]; > for i=1:M > for j=1:M > if (dis2(i,j)==1) row=i; > col=j; > eligible(counter,1)=row; > eligible(counter,2)=col; > counter=counter+1; > end > end > end
I understand you did it that way...but the point is twofoldfirst the line
eligible=[10,2];
at the beginning serves absolutely no useful purpose. It does _NOT_ allocate an array of 10x2 and the values that are stored there are wiped out if there even a single value found that satisfies the if() condition. Also, if there weren't any found, then your function would return the indication that there was a single value find at row 10, column 2 which is simply wrong in that instance. Try your implementation on an array
dis2=rand(10);
as input to it and see what it returns.
Also, even if it _did_ preallocate a 10x2 array, if there were more than a total of one '1' per line in the dis2 array, there would be a bounds error when the eleventh element was found.
What you really need instead at that point to help would be
eligible=zeros(M*M,2);
to preallocate the maximum size of the returned array. Of course, this is bigger than needed for every case except the one of all elements of dis2 == 1, but it's the only way to ensure not going outside the bounds of the array if you preallocate a fixed size w/o adding the logic to test and dynamically reallocate during the loop.
The problem w/ _not_ preallocating is that while for such a small M the overhead won't be particularly noticeable, in general "growing" such memory in a loop in Matlab will become a big overhead as problem sizes grow and therefore it's best practice to preallocate when at all possible.
In this case you could preallocate to M*M rows then truncate the trailing unused when the loop finishes.
But, the second point is that Matlab provides vectorized functional forms and a major if not the whole point of there _being_ a Matlab is to utilize that builtin functionality. It shortens up source code and as problem sizes grow it will in general help performance immeasurably.
So, I still submit the "Matlab way" is
[e(:,1) e(:,2)]=find(x==1);
If all you need is the locations and order isn't important and then
e=sortrows(e);
if the order by row _is_ significant. Or, rearrange x first, of course.
I've shown the coding above the demonstrates the two results are the same, identically.




