Ok!

Here is a simple problem for you. You have tow lists each containing 10 items. The first contains the values of the temperature in Celsius. The second list contains the equivalent value of the temperature in Fahrenheit for each one of those in the first list. Let say that you want to display those temperatures in the following format:

Celsius Fahrenheit
20.00 68.00
21.00 69.80
.
.
.
30.00 86.00

So, how can you do this?

well, this is a simple problem. However, the solution of this demonstrates the power and the efficiency of MATLAB’s built in functions.

The simplest solution would be using a loop that iterates through the values of each list and print out each value using a fprintf function. Here is the code:


cel = 20:1:30; % celsius temps.
fah = 9 .* cel ./ 5 + 32; % fahrenheit temps.

tic
fprintf(‘Celsius\tFahrenheit\n’);
for i = 1:size(cel,2)
fprintf(‘%-7.2f\t%-10.2f\n’,cel(i), fah(i));
end
toc

Note the tic .. toc combination that I used in the beginning and the end of the above code. This will time how long every thing between the tic .. toc took to in my machine. The output of the above is shown below:


Celsius Fahrenheit
20.00 68.00
21.00 69.80
22.00 71.60
23.00 73.40
24.00 75.20
25.00 77.00
26.00 78.80
27.00 80.60
28.00 82.40
29.00 84.20
30.00 86.00
Elapsed time is 0.002264 seconds.

So the output is as expected and it took about 0.002 seconds to compute in my machine (Dual Core i3 @ 2.2GHz processor).

Well, the fprintf is similar the C printf function. So, those who are familiar with C will recognize this function. However, MATLAB fprintf has a nice feature. I could give fprintf a list of values and it will executed for each item in the list. So the above solution could be written as follows:

tic
fprintf('Celsius\tFahrenheit\n');
fprintf('%-7.2f\t%-10.2f\n',cel,fah);
toc

The above code produces the following output:
Celsius Fahrenheit
20.00 21.00
22.00 23.00
24.00 25.00
26.00 27.00
28.00 29.00
30.00 68.00
69.80 71.60
73.40 75.20
77.00 78.80
80.60 82.40
84.20 86.00
Elapsed time is 0.000801 seconds.

This is nearly four times faster that using the loop. However, did you see the values? well look again. The values are not printed in the way I want. This is because MATLAB appends the second list to the first and prints the values from the first to the last until in runs out of values. This clearly not what I want. At a second though I took the transpose of the two lists and a put them in 10X2 matrix as follows:

tic
fprintf('Celsius\tFahrenheit\n');
fprintf('%-7.2f\t%-10.2f\n',[cel' fah']);
toc

Well, this is was no good as well. It happened that MATLAB process the matrix column wise and not row wise as I wanted. So a matrix that looks like this:

20.00 68.00
21.00 69.80
22.00 71.60

will be printed like this in MATLAB:

20.00 21.00
22.00 68.00
69.00 71.00

To get around this I took the transpose of the entire matrix that I created in last code listing:

tic
fprintf(‘Celsius\tFahrenheit\n’);
fprintf(‘%-7.2f\t%-10.2f\n’,[cel’ fah’]’);
toc

This has produced the following output:

Celsius Fahrenheit
20.00 68.00
21.00 69.80
22.00 71.60
23.00 73.40
24.00 75.20
25.00 77.00
26.00 78.80
27.00 80.60
28.00 82.40
29.00 84.20
30.00 86.00
Elapsed time is 0.000852 seconds.

Achieving both correctness and efficiency !

Have fun !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s