Deathslayer7
2010-11-20, 03:13 PM
The Question:
Approach
1. First, read the provided matrix from the excel sheet (name it Table).
2. It is recommended that you split the matrix into two different vectors (call the first vector numbers and the second digits).
3. Apply your programming skills and your knowledge of MATLAB commands to round the numbers in the vector numbers to their corresponding digits in the vector digits.
4. When you round the numbers, add a new column to the existing matrix Table and put your rounded answers in it.
5. Write the new matrix to the same Excel document to a sheet named YourLastName, as shown in Figure 1 (b).
Considerations
1. Your code should deal with any matrix size, so don’t write a code for 18x2 matrix!
2. Please attempt this project individually. You will be asked to submit this project in person, and a short discussion on the code details is expected.
3. At any point in this code you shouldn’t use any built-in functions (such as: round, ceil, floor, fix, max, min, …etc).
4. You can ONLY use (length, size, relational operators {< > ~=}, logical operators {|, &}, if, for, while and switch).
The code:
clc,clear,format short g,format compact
A=xlsread('Project.xlsx','numbers')
t=A(:,2)
l=length(t)
for i=1:l
B=A(:,1)./t
C(i)=B(i)
N(i)=B(i)
if B(i)<0
B(i)=-B(i)
else
B(i)=B(i)
end
if C(i)<0
C(i)=-C(i)
else
C(i)=C(i)
end
while B(i)>1
B(i)=B(i)-1
end
if B(i)>=.5
D(i)=B(i)+(1-B(i))
B(i)=C(i)+D(i)-B(i)
elseif B<.5
D(i)=B(i)-B(i)
B(i)=C(i)-B(i)
end
if N(i)<0
B(i)=-B(i)
else
B(i)=B(i)
end
number(i)=B(i)*t(i)
end
The problem I'm having is that my code will round numbers up but it wont round down.
For example given 8.4726 and round it to the 1's value, it gives me an output of .4726. That tells me that my rounding up works fine but my rounding down doesnt. Can anyone find the mistake? It would be greatly appreciated.
And I'm using MATLAb if it's needed to be known.
Thanks. :smallsmile:
edit: here's a better picture of the code
http://i198.photobucket.com/albums/aa125/death_slayer7/code.png
Approach
1. First, read the provided matrix from the excel sheet (name it Table).
2. It is recommended that you split the matrix into two different vectors (call the first vector numbers and the second digits).
3. Apply your programming skills and your knowledge of MATLAB commands to round the numbers in the vector numbers to their corresponding digits in the vector digits.
4. When you round the numbers, add a new column to the existing matrix Table and put your rounded answers in it.
5. Write the new matrix to the same Excel document to a sheet named YourLastName, as shown in Figure 1 (b).
Considerations
1. Your code should deal with any matrix size, so don’t write a code for 18x2 matrix!
2. Please attempt this project individually. You will be asked to submit this project in person, and a short discussion on the code details is expected.
3. At any point in this code you shouldn’t use any built-in functions (such as: round, ceil, floor, fix, max, min, …etc).
4. You can ONLY use (length, size, relational operators {< > ~=}, logical operators {|, &}, if, for, while and switch).
The code:
clc,clear,format short g,format compact
A=xlsread('Project.xlsx','numbers')
t=A(:,2)
l=length(t)
for i=1:l
B=A(:,1)./t
C(i)=B(i)
N(i)=B(i)
if B(i)<0
B(i)=-B(i)
else
B(i)=B(i)
end
if C(i)<0
C(i)=-C(i)
else
C(i)=C(i)
end
while B(i)>1
B(i)=B(i)-1
end
if B(i)>=.5
D(i)=B(i)+(1-B(i))
B(i)=C(i)+D(i)-B(i)
elseif B<.5
D(i)=B(i)-B(i)
B(i)=C(i)-B(i)
end
if N(i)<0
B(i)=-B(i)
else
B(i)=B(i)
end
number(i)=B(i)*t(i)
end
The problem I'm having is that my code will round numbers up but it wont round down.
For example given 8.4726 and round it to the 1's value, it gives me an output of .4726. That tells me that my rounding up works fine but my rounding down doesnt. Can anyone find the mistake? It would be greatly appreciated.
And I'm using MATLAb if it's needed to be known.
Thanks. :smallsmile:
edit: here's a better picture of the code
http://i198.photobucket.com/albums/aa125/death_slayer7/code.png