Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Matlab halp

Name: Anonymous 2010-10-29 2:39

Can anyone here help me with making a matlab function, it's an exercise yes, but I don't really know much of recursions.

Here's what I have to do:
Given two integers, write a function, recursiveProduct, that calculates the product of the two numbers.

You *must* use recursion.
You may *not* use the * or .* operators. 
You may *not* use the prod() function.
You MAY assume that the second integer will not be negative

Name: Anonymous 2010-10-29 2:59

Read SICP.

Name: Anonymous 2010-10-29 3:04

:|

So no one can help me with this?

Name: Anonymous 2010-10-29 3:08

>>3
// Assume b >= 0.
int recursiveProduct(int a, int b)
{
    return b == 0 ? 0 : a + recursiveProduct(a, b-1);
}

Name: Anonymous 2010-10-29 3:10

Wait what's that?

I need to write a matlab function, I'm not sure if that's one.

A matlab function for this would look like

function product = recursiveProduct(int1, int2)

code

end

Name: Anonymous 2010-10-29 3:11

>>3
As a matter of fact, >>2 did help you. There's an answer in SICP, with a nice chart and all (be sure to read the errata, there's an error in the chart!).

Name: Anonymous 2010-10-29 3:18

Students get dumber and lazier every year. Must be something in the water.

Name: Anonymous 2010-10-29 3:18

product(F1,0,0) :- !.
product(F1,F2,P) :-
     F3 is F2-1,
     product(F1,F3,P2),
     P is P2+F1.

Name: Anonymous 2010-10-29 3:24

>>8

What is that shit?

I've never seen anything like that in Matlab before.

Name: Anonymous 2010-10-29 3:27

>>9
When will you learn to sage?

Name: Anonymous 2010-10-29 3:29

The real answer is that you've been given the answer thrice — one in algorithmic form (>>2), one in a language you should know (>>4), and heck, even one in a toy language if you're into that (>>8). If you can't turn that into an algorithm in any language, then perhaps it's time to find a new hobby.

Name: Anonymous 2010-10-29 3:34

Easy enough, except I don't know matlab, so here's a CL solution:

(defun multiply (x y)
  (case y
    (0 0) (1 x) (-1 (- x))
    (t (+ (if (zerop (logand y 1)) 0 x)
          (multiply
           (ash x 1)
           (ash y -1))))))

Works with negative numbers too (or any fixnum/bignums).
Add a local function for accumulating the results if you want to accumulate the result (take advantage of TCO).
I believe an iterative solution would be fine here too.

Here's a simple C solution too (much shorter since C has special syntax for shifts/arith ops):

int multiply(int x, unsigned int y)
{
    int product;

    for (product=0;y;x<<=1,y>>=1)
      if (y&1) product+=x;
    return product;
}


I was going to write a x86 assembly version too, but looking at the compiled version of the previous function, I decided against it (it was good enough):

;; the body of the function taking 2 arguments, cdecl convention
mov  ecx, dword ptr [esp+8]
xor  eax, eax
test ecx, ecx
jz done
mov edx,[esp+4]

next_y:
test cl,1
jz low_bit_unset
add eax,edx
low_bit_unset:
shl edx,1
shr ecx,1
jnz next_y

done:
retn


I could probably write a VHDL implementation of it too, since that's usually a common part of an ALU, and most of the time the only sensible place you want to implement this (why bother when most CPUs and languages have multiply instructions provided for you? They are much more efficient because they're implemented in hardware, and in cases when you're multiplying by a constant, the compiler can just optimize it all into shifts/adds).

Name: Anonymous 2010-10-29 4:27

>>10
never and your gay

Name: Anonymous 2010-10-29 4:33

halp

Stopped reading right there.

Name: Anonymous 2010-10-29 4:39

This thread has been closed and replaced with the following thread:

Subject:
Paradox halp
Name:
Email:


Can anyone here help me with solving a paradox, it's an exercise yes, but I don't really know much of paradoxes.

Here's what I have to do:
Given two options, make a choice between never having sex again and never programming again.

You *must* make the choice
You may *not* choose both.
You may *not* choose none.
You MAY assume that you have both programmed and had sex already.

Name: Anonymous 2010-10-29 4:41

You MAY assume that you have both programmed and had sex already.
Can I assume I haven't done either?

Name: Anonymous 2010-10-29 4:45

function [res] = myProduct(a,b)
   if b <= 1
      res = a;
   else
      res = a+myProduct(a,b-1);
   end
end


I probably fucked that up, but you get the point. There you go. Precisely what your teacher wants to see. Now you can go throw a ball or something

Name: Anonymous 2010-10-29 4:52

>>17
It's still O(n), when it can easily be done in O(logn).

Name: Anonymous 2010-10-29 4:57

>>18
Yes of course

Name: Anonymous 2010-10-29 4:58

>>17
Shit like this only encourages these morons.

Name: Anonymous 2010-10-29 5:20

>>20
And I just love pissing off faggots like you.

Name: Anonymous 2010-10-29 6:47

No solutions that handle real numbers?

Name: Anonymous 2010-10-29 8:49

>>22
As long as it handles anuses, it can be adapted to solve any number of different problems, even problems that previously weren't problematic.

Name: Anonymous 2010-12-08 19:38

<

Don't change these.
Name: Email:
Entire Thread Thread List