From: Adnane on 8 Mar 2010 22:39 Hi everyone, I'm sorry if this is redundant but I've gone through the forum and Matlab documentation without finding an answer to my problem. I'm building a epidemiology model to study plant-pathogen interactions. Most of my code is vectorized except for the main loop. In a nutshell, I'm trying to access the cells that carry a population, do some modifications to them without resorting to my current for loop. The functions go something like this: function [y1,y2,y3] = sub1(metapop, mat2, param, test1) z=find(test1,1); %test1 tells me if I will do something on that population for temp=1:size(z,1) pop=z(temp,1); m1= metapop(pop).population; m2= mat2(:,pop); m1=some_function1(m1,m2); metapop(pop).population=m1; param.data(pop,param.variable1)=1; param.data(pop,param.variable2)= some_function2(m1); end y1=metapop; y2=param.data; y3=[]; So essentially, I select the indices of the structure array I want to include in the loop. Then I do some calculations on metapop(pop).population for all selected populations and record some output in another matrix (param.data). Any help would be greatly appreciated. Thanks! Adnane
From: TideMan on 9 Mar 2010 03:02 On Mar 9, 4:39 pm, "Adnane " <haryu...(a)gmail.com> wrote: > Hi everyone, > I'm sorry if this is redundant but I've gone through the forum and Matlab documentation without finding an answer to my problem. I'm building a epidemiology model to study plant-pathogen interactions. Most of my code is vectorized except for the main loop. > In a nutshell, I'm trying to access the cells that carry a population, do some modifications to them without resorting to my current for loop. > The functions go something like this: > > function [y1,y2,y3] = sub1(metapop, mat2, param, test1) > z=find(test1,1); %test1 tells me if I will do something on that population > for temp=1:size(z,1) > pop=z(temp,1); > m1= metapop(pop).population; > m2= mat2(:,pop); > m1=some_function1(m1,m2); > metapop(pop).population=m1; > param.data(pop,param.variable1)=1; > param.data(pop,param.variable2)= some_function2(m1); > end > y1=metapop; y2=param.data; y3=[]; > > So essentially, I select the indices of the structure array I want to include in the loop. Then I do some calculations on metapop(pop).population for all selected populations and record some output in another matrix (param.data). > > Any help would be greatly appreciated. Thanks! > Adnane And the problem is...............? You know, oftentimes a loop is the best way. But if there's a bottleneck somewhere, you can check it out using profile.
From: Adnane on 10 Mar 2010 00:59 Sorry but your comment isn't very helpful. When you say:"You know, oftentimes a loop is the best way.", do you mean there's no obvious trick to speed up this particular kind of loop whether with vectorization or otherwise ? The title of my request was "speeding access to a structure array", so I'm interested in finding a way if such way exists. For my specific purpose, not in general, "you know"...
From: Jan Simon on 10 Mar 2010 04:09 Dear Adnane! > function [y1,y2,y3] = sub1(metapop, mat2, param, test1) > z=find(test1,1); %test1 tells me if I will do something on that population > for temp=1:size(z,1) "z = find(test1, 1)" replies the index of the first non-zero element. SIZE(z, 1) is 1 ever, so you can omit the FOR loop completely. There is no need or potential to accelerate the loop in consequence. If y3 is the empty matrix ever, remove it from your program. Kind regards, Jan
From: Adnane on 10 Mar 2010 19:14 Jan- you're right. find(test1,1) is 1. I made a mistake while rewriting my function to post it on the forum. It's actually find(test1) with test1 being a 10000x1 matrix with 0's and 1's so i still need the for loop. Any advice ?
|
Next
|
Last
Pages: 1 2 Prev: Modifying parameters from power_FCV_powertrain.mdl Next: segmentation fault from saveas |