Perms output as trials to solve an equation

Is there a way to use the perms output as the variable values of an equation and identify a unique solution?
For example, perms(1:2) gives [2 1; 1 2]
If I have an equation 4x + 2y, can I use the different values (x=2, y=1 and x=1, y=2) in an equation to determine a desired solution (i.e. 10)?
If I wanted to have a final value of 10, the identified values would x=2 and y=1.

Antworten (2)

Image Analyst
Image Analyst am 30 Apr. 2014

0 Stimmen

I would not think so. What makes you think that you would happen to have the solution already in your list of numbers that you're going to make permutations of? If you want to do it numerically, you could use linspace() to make a list of values for x and y, then use meshgrid() to get every (x,y) coordinate in the space, then plug those x,y into your equation, then search for the coordinate that has a value closest to your desired value (10) with min() and ind2sub.
% Make x and y axes.
x=linspace(1,10,100);
y = linspace(1, 20, 100);
% Get x and y over the entire grid.
[xg, yg] = meshgrid(x, y);
% Make the equation for every x and y.
z = 4*xg + 2* yg
% Display it.
image(z);
colormap(gray);
colorbar
% Find where target value lives.
targetValue = 10;
diffMatrix = abs(z - targetValue);
[minDiff, linearIndex] = min(diffMatrix(:))
[row, col] = ind2sub(size(z), linearIndex)

7 Kommentare

Image Analyst
Image Analyst am 30 Apr. 2014
B's "Answer" moved here:
Thanks for your reply.
I have an equation that has 6 variables in it, and the variables can take on the unique values 1 through 6. There are 720 permutations (6!) of values that the variables can take on, and I know that one arrangement will produce the unique solution that I'm looking for (final value of 10).
I'm hoping there's an easy way to determine the values of the variables which results in an answer of 10.
Image Analyst
Image Analyst am 30 Apr. 2014
Well then I think that might work, though of course you could also use 6 nested for loops to do the same thing. Not sure which would be faster, though with only 720 they'll both be incredibly fast and you probably won't notice a difference.
B
B am 1 Mai 2014
I visualized something similar to solving linear equations using matrices - something along the lines of:
[A]*[B], where [A] was a 720x6 matrix (6!), and [B] was a 6x1 matrix ([a,b,c,d,e,f]'). I wanted to have the system multiply itself out until it came up with the value of 10 for the equation that I'm using.
Star Strider
Star Strider am 1 Mai 2014
Wouldn’t the matrix equation Ax = y work with gcd to help identify the integers? (Or maybe I’m once again missing something.)
Image Analyst
Image Analyst am 1 Mai 2014
Do you fully understand this section of the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F and how you need to test floating point numbers for "matching" values?
B
B am 1 Mai 2014
Bearbeitet: B am 1 Mai 2014
That FAQ section makes perfect sense. I'm great with the conceptualization of what needs to be done, I just don't know how to implement it. Also, all of the numbers I'm using (both variables and coefficients) are integers, so I'm not worried about issues with floating point numbers.
My equation is -402u^6 + 67v^5 + 48w^4 + 295x^3 - 720y^2 + 1145z - 14648, and the values of u, v, w, x, y, z can only be 1-6.
At the very inside of the 6 for loops, do this:
value = -402u^6 + 67v^5 + 48w^4 + 295x^3 - 720y^2 + 1145z - 14648;
if value == 10
foundAnswer = true;
message = sprintf('It is 10 for u=%d, v=%d, w=%d, x=%d, y=%d, z=%d',...
u,v,w,x,y,z);
uiwait(helpdlg(message));
break;
end
Before the 6 for loops have
foundAnswer = false;
and then at the end of each for loop, just before its end statement, you'll have:
if foundAnswer = true;
break;
end

Melden Sie sich an, um zu kommentieren.

B
B am 3 Mai 2014

0 Stimmen

Any help would be appreciated.

Kategorien

Gefragt:

B
B
am 30 Apr. 2014

Kommentiert:

am 3 Mai 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by