0

I would like to make an optimisation by avoiding to use theIf else i used this element because when E = 0in the logic of my probelm B = [0 0 0 0 0].

By consequence I have an error Nan . I suppose it is because i have R/0 at i =1 and 2

% i reflects the time
% boolean flag type double
B = [0 0 0 0 0
     0 0 0 0 0
     0 0 0 0 1
     0 0 0 1 0
     0 0 0 1 1];
% info1
E = [0 0 10 20 40];

% info2
R(1:5) = 1/30;

powerload_R2 = zeros(5);

for i =1:5
    if E(i)>0
       powerload_R(i,:) = R ./ dot(R,B(i,:)) .* B(i,:)*E(i); % fonctionnel
    else
       powerload_R(i,:) = 0;
    end
    powerload_R2(i,:) = R ./ dot(R,B(i,:)) .* B(i,:)*E(i); %
end
%results

%what  i get with :  
powerload_R(i,:)=

0   0   0   0   0
0   0   0   0   0
0   0   0   0   10
0   0   0   20  0
0   0   0   20  20

powerload_R2(i,:)=
NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN
0   0   0   0   10
0   0   0   20  0
0   0   0   20  20
Johnvador
  • 21
  • 5
  • The general advice for computational problems like this is to have the if else. Or otherwise, in the end, fill NaNs with zeros, if that works for you. Avoids the if – Ander Biguri Apr 20 '22 at 22:46
  • thank you for your input. i will try to find a solution to replace `NAN` by `0`. Do you have any suggestion ? – Johnvador Apr 21 '22 at 07:16
  • thank you for your input. i will use one solution given for sackloverflow team – Johnvador Apr 22 '22 at 10:50

2 Answers2

1

The NaNs appear from 0/0 division, so only add eps to the denominator to avoid this and get 0/eps = 0 instead.

% i reflects the time
% boolean flag type double
B = [0 0 0 0 0
     0 0 0 0 0
     0 0 0 0 1
     0 0 0 1 0
     0 0 0 1 1];
% info1
E = [0 0 10 20 40];

% info2
R(1:5) = 1/30;

powerload_R  = zeros(5);
powerload_R2 = zeros(5);

for i = 1:5 
    if E(i) > 0
        powerload_R(i,:) = R ./ dot(R,B(i,:)) .* B(i,:)*E(i); % fonctionnel
    end
    powerload_R2(i,:) = R ./ (dot(R,B(i,:))+eps) .* B(i,:)*E(i); %
end
AboAmmar
  • 5,439
  • 2
  • 13
  • 24
0

Just for fun, the vectorized version of your formula is:

powerload_R = R ./ (B*R.') .* (B.*E.');

This results in:

powerload_R =
   NaN   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN   NaN
     0     0     0     0    10
     0     0     0    20     0
     0     0     0    20    20

We can make a logical array to tell calculate only the rows where E is non-zero.

E_NZ = E ~= 0;

Then use E_NZ to index B and E:

powerload_R(E_NZ,:) = R ./ (B(E_NZ,:)*R.') .* (B(E_NZ,:).*E(E_NZ).');

So the complete script would look like this:

% i reflects the time
% boolean flag type double
B = [0 0 0 0 0
     0 0 0 0 0
     0 0 0 0 1
     0 0 0 1 0
     0 0 0 1 1];
% info1
E = [0 0 10 20 40];

% info2
R(1:5) = 1/30;

powerload_R  = zeros(5);
E_NZ = E ~= 0;

powerload_R(E_NZ,:) = R ./ (B(E_NZ,:)*R.') .* (B(E_NZ,:).*E(E_NZ).');

The result is the same matrix without the NaN values and no (explicit) looping:

powerload_R =
    0    0    0    0    0
    0    0    0    0    0
    0    0    0    0   10
    0    0    0   20    0
    0    0    0   20   20
beaker
  • 16,331
  • 3
  • 32
  • 49
  • what if i have to add the info powerload_R (5,5) with powerload_R (5,4) (indexing current valu with previous value) – Johnvador Apr 22 '22 at 12:00
  • @Johnvador I'm not sure I understand your question. The obvious answer is `powerload_R(5,5) + powerload_R(5,4)`, but I doubt that's what you're asking. Do you want only the last two values in the matrix? Are you adding new requirements? Both of the answers given so far produce the results you ask for in your question. If you have another question, you can put it in a new post. – beaker Apr 22 '22 at 15:05
  • in ` (B(E_NZ,:)*R.')` Does ` E_NZ,` mean the line 1 then line 2 then ine 3 .... (B (lineEz,:)*R. for B or do it mean the value of EZ is used to be multiplie to B for exemple for R1 `0 * [0 ]` *R1 then R5 = 0 * [1]*R5. EDIT I jsut saw your answaer i will post an edit tonight in the original post – Johnvador Apr 22 '22 at 15:31
  • `B(E_NZ,:)*R.'` means that only the rows where `E_NZ` is true (where `E` is nonzero) will be used in the calculation. – beaker Apr 22 '22 at 15:43
  • For more information on logical indexing, see [Luis Mendo's excellent Q&A](https://stackoverflow.com/questions/32379805/linear-indexing-logical-indexing-and-all-that). – beaker Apr 22 '22 at 16:34