1

I am trying to compute x1^i * x2^j * x3^k * ......

This is my code so far:

for l = 1:N
f = 1;
for i = 0:2
    for j = 0:2-i
        for k = 0:2-j
            for m = 0:2-k
                g(l,f) = x1(l)^i*x2(l)^j*x3(l)^k*x4(l)^m;
                f = f+1;
            end
        end
    end
end
end

How can I do this easier or without a loop?

1 Answer 1

1

I do not have MATLAB on hand here, but what I'd do is make a vector X = [x1, x2, ..., xn] of bases and a vector P = [i, j, k, ..., z] of powers, and then compute prod(power(X, P)).

power() does an element-wise power function, and prod takes the product of every element in the vector.

Sign up to request clarification or add additional context in comments.

5 Comments

Answer is that Just Do consist of P. wow thank you so much! but I need recursive loop for P.... T_T
I find it really hard to understand exactly what you want to do... :(
Following your answer, I need to make vectors P. For example, for three variable, p(1)=[0 0 0], p(2)=[0 0 1], p(3)=[0 0 2], p(4)=[0 1 0], p(5)=[0 1 1], p(6)=[0 2 0], p(7)=[1 0 0], p(8)=[1 0 1], p(9)=[1 1 0], p(10)=[2 0 0]. When I calculate many variable(x1, x2, x3, x4, x5, ..., x100), how to make vectors P? maybe I need recursive loop. sorry my poor explanation.. OTL
It seems your vector P is the ternary (like binary or decimal, only base 10) representation of the index. Try looking at dec2base.
Thank you for your help! Your dec2base idea is great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.