-1

In my data let say I have two vectors:

column1  column2 

40.0000    1.2000
41.0000    1.2000
42.0000    1.2000
43.0000    1.2000
44.0000    1.2000
45.0000    1.2000
46.0000    1.2000
47.0000    1.2000
48.0000    1.2000
49.0000    1.2000
50.0000    1.2000

where column1 is temperature and column2 is flow rate.

What I want is to identify what is the sum of the flow rate for conditions when the temperature is greater than 45 degrees Celsius. i.e. in the above example sum flow rates in column2 only with condition when the temperature in column1 is greater than 45 degrees Celsius.

How can I do this?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
D_Marini
  • 19
  • 3
  • 2
    If any of the three answers you received was of help, please consider accepting one. This will get both you and the answerer a bit of reputation and signifies that you no longer require help with this question. There's no obligation to do so, see [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Adriaan Sep 22 '16 at 11:12

3 Answers3

6
sum(columns2(column1>45))

use logical indexing! What this does, is that column1>45 creates a logical matrix which is true for values of column1 which are greater than 45 and false when not. This logical array can then be used to directly index the other column.

If you indeed have a 2D matrix instead of 2 vectors, simply use the first column to logically index the second:

sum(data(data(:,1)>45),2)
Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
1

sum(columns2(column1>45)): the best way and the more compact way to do this.

If you want the "naive-way" more similar to the classical languages and understandable:

acc=0;
for ii=1:lenght(column1)
    if column1(ii)>45
       acc=acc+column2(ii);
    end
end
Leos313
  • 5,152
  • 6
  • 40
  • 69
  • 4
    More "understandable" is rather debatable, it however is a lot slower in MATLAB, since MATLAB excels in *Matrix operations* (hence the name MATLAB). Besides, [using `i` as a variable](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) can be frowned upon in MATLAB as well. – Adriaan Sep 22 '16 at 09:58
  • 1
    of course I agree completly. The second code is "just-to-understand" because the question is from somebody that doesn't know the power of Matlab as we do. In any case, if you want to be fast, Matlab is far far away to be a good choice. – Leos313 Sep 22 '16 at 10:02
  • 3
    Not necessarily a [bad choice](http://stackoverflow.com/q/6058139/2338750). You must be very skilled in order to outperform many of MATLAB's builtin, precompiled, heavily optimized functions. But yes, MATLAB is slower than many low level languages, if the programmer is skilled. – Stewie Griffin Sep 22 '16 at 10:56
  • Dear Adrian, Thank you very much for the e-mail. What you suggested it is working and doing exactly what I wanted. similar to this I wanted to sum the flow rate at temperature intervals at 2 celcius increment. For example, I want that index to identify temperature >37 then >39 then > 41 ….. >51. I can repeat the loop for each 2 degree celcius increment but it will take a lot of time for calculations. Is there anything that can be added in the loop which you think it can accomplisht the tasck for a shorter time? Thanks Dashamir – D_Marini Sep 23 '16 at 10:39
  • @D_Marini I suppose you tried to ping me. First, I didn't email anyone. Second, what you are asking is a separate question. Please mark any of these three answers as accepted, then [ask](http://stackoverflow.com/questions/ask) a new question with your new problem. – Adriaan Sep 23 '16 at 10:49
  • sorry, it was mean to Leos response – D_Marini Sep 23 '16 at 11:57
  • I don't know if I have understand so good but, if you want to repeat the same set of instruction for different values of temperatures, you can use the same for loop: `acc=0; for ii=1:lenght(column1) if column1(ii)>45 acc=acc+column2(ii); end if column1(ii)>37 acc=acc+column2(ii); end ...%add here other threshold end`. If you ask another question I can re-write the answer in order to read it better. – Leos313 Sep 23 '16 at 12:11
  • Well, You must use two acc: `acc1=0; acc2=0; for ii=1:lenght(column1) if column1(ii)>45 acc1=acc1+column2(ii); end if column1(ii)>37 acc2=acc2+column2(ii); end %add here other threshold... end`. – Leos313 Sep 23 '16 at 12:18
0

This code should work:

sum(flowrate(temperature > 45))
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
anzu
  • 1
  • 5
    Why does it work? What here is code, what is not? Please take care to format your answers properly, and refrain from posting code-only answers without explanation, as they will be of no use to future readers. – Adriaan Sep 22 '16 at 09:49