Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

reduce computation time for using a matrix with nan

$
0
0
Dear ALL,

I have a matrix x(750,2000) got from a cell r{} with size (1*6)which has the following format:

       1 2 nan nan nan 4 5 6....
       1 nan 2 nan 4 nan5 6....
       4 5 2 nan 4 nan5 6....
         ...
        4 5 6 1 2 nan nan nan....
       nan 4 nan5 1 nan 2 6....
       4 5 2 nan 4 nan5 6....
I want to calculate a function for the non-nans on each row and then get the mean value for the results of the functions (from matrix row calculation). Below is my code:

for i=1:length(r)
x=r{i} ;
 
 x2=nan(750,1);
 for ii=1:size(x,1)
     x1=x(ii,:);
 x1=x1(~isnan(x1));
 if ~isempty(x1)
 x2(ii,1)=(sum(x1)^3-3*sum(x1.^2)*sum(x1)+2*sum(x1.^3));
 end

The problem is the computation time is huge since in one loop I need 1hour to do so, but without this step it took me only 4min... Since I have 200 loops to run reducing this step's computation time will be extremely helpful, can anyone help me about this?

Thanks a lot in advance

Viewing all articles
Browse latest Browse all 19628

Trending Articles