PDA

View Full Version : Programming Help Please and Thank You.



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

HypoSoc
2010-11-20, 03:22 PM
I don't know matlab, but this line:

D(i)=B(i)-B(i)
Seems especially strange to me.
Doesn't that make D(i) equal to 0 only? Its in the less than .5 so that might be the problem.
Also, what is D(i)? I don't see it used anywhere before or after that.

Eloel
2010-11-20, 03:24 PM
D(i)=B(i)+(1-B(i))
Also this, seems unnecessary.
D(i) = 1
Right?

Edit: Forgot to mention I don't know MATLAB. Just speculating.

Deathslayer7
2010-11-20, 03:24 PM
@ hyposoc:i was actually thinking and that line isnt even needed at all. I ran the program without it and it still runs the same.

Also, that line defines D(i)=0 for all numbers less that are less than .5. But then after that it isnt even used.

Deathslayer7
2010-11-20, 03:26 PM
Also this, seems unnecessary.
D(i) = 1
Right?

actually that line is needed. because we add one to the number when rounding up which would be D(i) then subtract the remainder to get it to round nicely.

or in more practical terms. B=8.6
B=.6
D=1
C=B=8.4 (we established that before B got subtracted.)

then number =C+D-B=8.6+1-.6 =9

Deathslayer7
2010-11-20, 03:45 PM
Thank you guys, but i think i got it. The problem was in line 25 with the elseif. It should just be else, and it worked! I think. I'll have to use more numbers to make sure.

RS14
2010-11-20, 03:47 PM
I know Matlab pretty well, but I can't make any sense of it without knowing the format of the contents of the .xlsx file.

You should also comment your code and use meaningful variable names, to make it easier for people to understand.

Specifically, what are B,C,D, and N?

Also, you should probably be preallocating your matrices. This is because, in Matlab, it is slow to grow a matrix on each iteration of the loop, as you do with N; better, if you can do so, is to write e.g.

N=zeros(a,b)
% a and b are the expected final dimensions of N
for i = 1:l

N(i)=f(x)
end

Deathslayer7
2010-11-20, 03:54 PM
the contents of the excel file is just a matrix and I got the code working. :smallsmile:

Also rewrote it back into the .xlsx file so now I'll take your advice and make it a bit cleaner.

Thanks for the help.

factotum
2010-11-20, 05:20 PM
I also don't know Matlab, but I'd be astonished if it didn't have a built-in function for rounding numbers rather than you having to do this horrible if...else structure to do the job!

RS14
2010-11-20, 05:24 PM
I also don't know Matlab, but I'd be astonished if it didn't have a built-in function for rounding numbers rather than you having to do this horrible if...else structure to do the job!


At any point in this code you shouldn’t use any built-in functions (such as: round, ceil, floor, fix, max, min, …etc).

Ilovewhitetext

Eloel
2010-11-20, 05:33 PM
Ilovewhitetext


2. Please attempt this project individually
If you want rules, you get rules

Deathslayer7
2010-11-20, 05:40 PM
individually meaning classmates :smalltongue:

but either way i had 95% of it done, just that dumb else/elseif was killing me and i still got it on my own.