function [cMatrix] = makeC (n,f)%n is the degree of the polynomial, f is the number of factors (dimensionality)
A = [];if f == 1 cMatrix = 2*rand(n+1,1)-1;else for i = 1:f A(1,i) = n+1; end cMatrix = rand(A);endfor i = 1:numel(cMatrix) coords = ind2sub(size(cMatrix),i); coordSum = 0; for ii = 1:numel(coords) check = check + coords(ii); if coordSum < f(n+1) - n cMatrix(i) = 0; end endend
The problem is that ind2sub (a built-in matlab function that converts a linear index into subscripts) does not produce the correct number of coordinates on its own. I have to say something like:
[x , y] = ind2sub([2,2],3)to make [x,y] become [1 , 2]
but the number of dimensions in the multidimensional array is at the discretion of the user. So I need the output of ind2sub to be a 1xf matrix where f is the number of dimensions named coords. Currently coords is just the linear address! I am new to matlab (like as of yesterday) so please be thorough.
Possibly irrelevant information regarding the purpose of the function: generates a matrix of random coefficients for an nth order polynomial. The problem is that if you want a 2nd order polynomial with 2 factors (3×3 matrix) you don't want terms like x^2*y^2 because that is a quartic term. So the coef matrix for 2nd order 2 factors would look like:
x^2 x cy^2 0 0 rand# y 0 rand# rand# c rand# rand# rand#
The first half of the code makes the appropriately sized matrix the second half iterates through and finds where the zeros go.
Best Answer