Problem

MATLAB

Consider the following two vectors of temperatures in Celsius degrees to Fahrenheit, using a for-loop and then prints them on screen.

Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]
Fdegrees = [-20, -15, -5, 0, 10, 15, 30, 35, 40]


(A) Write a function that takes an input vector of temperatures, and a string which is either 'F2C' or C2F. Then, converts the input temperature vector from Fahrenheit to Celsius if the input string is 'F2C', otherwise converts the input temperature vector from Celsius to Fahrenheit if the input string is 'C2F', otherwise outputs an error message and aborts the program.

(A) Write this function using while-loop construct (you can name the function convertTempFor.m).
(B) Write this function using for-loop construct (you can name the function convertTempWhile.m).
(C) Write this function using vectorization concept (you can name the function convertTempVec.m).
(D) Use MATLAB built-in timing functions to measure the performance of the three functions in the above.

Here are some example calls to these functions,

InVec = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40];
convertTempFor(InVec,'C2F')
ans =
    -4     5    14    23    32    41    50    59    68    77    86    95   104
convertTempWhile(InVec,'C2F')
ans =
    -4     5    14    23    32    41    50    59    68    77    86    95   104
convertTempVec(InVec,'C2F')

ans = -4 5 14 23 32 41 50 59 68 77 86 95 104

Python

Consider the following example code, which converts a list of temperature values from Celsius to Fahrenheit, using a for-loop and then prints them on screen.

Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]
print ('    C     F')
for C in Cdegrees:
    F = (9.0/5)*C + 32
    print ('%5d %5.1f' % (C, F))
    C     F
  -20  -4.0
  -15   5.0
  -10  14.0
   -5  23.0
    0  32.0
    5  41.0
   10  50.0
   15  59.0
   20  68.0
   25  77.0
   30  86.0
   35  95.0
   40 104.0

Write a while-loop implementation of the above code.

Solution

MATLAB

Here are the three function,

  • (A) convertTempFor.m,

    function OutVec = convertTempFor(InVec,str)
      OutVec = zeros(size(InVec));
      if strcmp(str,'C2F')
          for i = 1:length(InVec)
              OutVec(i) = (9.0/5.0)*InVec(i) + 32.0;
          end
      elseif strcmp(str,'F2C')
          for i = 1:length(InVec)
              OutVec(i) = (5.0/9.0)*(InVec(i) - 32.0);
          end
      else
          error(['the requested conversion ',str,' is not supported. Program aborted.']);
      end
    end
    


  • (B) convertTempWhile.m,

    function OutVec = convertTempWhile(InVec,str)
      i = 1;
      OutVec = zeros(size(InVec));
      if strcmp(str,'C2F')
          while i <= length(InVec)
              OutVec(i) = (9.0/5.0)*InVec(i) + 32.0;
              i = i + 1;
          end
      elseif strcmp(str,'F2C')
          while i <= length(InVec)
              OutVec(i) = (5.0/9.0)*(InVec(i) - 32.0);
              i = i + 1;
          end
      else
          error(['the requested conversion ',str,' is not supported. Program aborted.']);
      end
    end
    


  • (C) convertTempVec.m,

    function OutVec = convertTempVec(InVec,str)
      if strcmp(str,'C2F')
          OutVec = (9.0/5.0)*InVec + 32.0;
      elseif strcmp(str,'F2C')
          OutVec = (5.0/9.0)*(InVec - 32.0);
      else
          error(['the requested conversion ',str,' is not supported. Program aborted.']);
      end
    end
    


(D) Here is one way, (timing.m script), of timing the functions,

InVec = randi(100,1,10^7);
disp(['Timing for convertTempVec: '  , num2str(timeit(@()convertTempVec(InVec,'C2F'))), ' seconds.'   ]);
disp(['Timing for convertTempFor: '  , num2str(timeit(@()convertTempVec(InVec,'C2F'))), ' seconds.'   ]);
disp(['Timing for convertTempWhile: ', num2str(timeit(@()convertTempWhile(InVec,'C2F'))), ' seconds.' ]);


Here is a test result,

timing
Timing for convertTempVec: 0.038723 seconds.
Timing for convertTempFor: 0.03936 seconds.
Timing for convertTempWhile: 0.18011 seconds.

The while-loop implementation is far slower than the vectorized and for-loop versions.

Python
Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]
index = 0
print ('    C     F')
while index < len(Cdegrees):
    C = Cdegrees[index]
    F = (9.0/5)*C + 32
    print('%5d %5.1f' % (C, F))
    index += 1
    C     F
  -20  -4.0
  -15   5.0
  -10  14.0
   -5  23.0
    0  32.0
    5  41.0
   10  50.0
   15  59.0
   20  68.0
   25  77.0
   30  86.0
   35  95.0
   40 104.0

Comments