Store variables from a loop to use later
Show older comments
Hi, I have this simplified version of a script I am trying to run. I have a range of variables I want to run through an equation ('Answer' in this case). I have determined that I only want solutions that produce a value less than 10, I want to store these values in an array so that I can check they provide the correct solution, along with allowing me to do further calculations later on. Currently I have used display to print the i,j,k values and then copy and paste them into a vector. This is rather tedious and I am struggling to get the values to be stored in an array.
clear,clc
w = [9 8 7];
x = [1 2 3];
y = [3 2 1];
Answer = zeros(length(w),length(x),length(y));
for i = 1:length(w)
for j = 1:length(x)
for k = 1:length(y)
Answer(i,j,k) = w(i)*x(j)*y(k);
if Answer(i,j,k) <= 10
disp([num2str(i),',',num2str(j),',',num2str(k)]) %displays the order of i,j,k that produces a desired solution
Var = zeros(length(Answer),length(Answer));
Var(i,:,:) = [i j k]; % Vector with variables in order than produces solution
end
end
end
end
for l = 1:length(Var)
for m = l+3
for n = m+6
Values = w(Var(l))*x(Var(m))*y(Var(n)); %Verify that the variables produce correct solution
end
end
end
Currently the i,j,k values that give a solution less than 10 are 1,1,3 | 2,1,3 | 3,1,3
When I try to store this in Var the array is rewriten each time meaning that I can only confirm the last configuration in the next for loop, i.e. Var = [0 0 0;0 0 0;3 1 3]
If anyone can help with this, it would be gladly appreciated
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!