Store variables from a loop to use later

32 vues (au cours des 30 derniers jours)
Brandon Ratter
Brandon Ratter le 11 Avr 2021
Commenté : Brandon Ratter le 11 Avr 2021
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

Réponse acceptée

David Fletcher
David Fletcher le 11 Avr 2021
Modifié(e) : David Fletcher le 11 Avr 2021
The line in the k loop:
Var = zeros(length(Answer),length(Answer));
This overwrites all values in Var with zeroes on every iteration of the k loop (except the last). The preallocation would need to be moved outside all loops (like your preallocation for the Answer var). Either that or just grow it dynamically - although it's not good programming practice as it has a lot of time and memory overhead, but as a quick hack to get things working it is an easy alternative, and unless the data set is large you probably won't notice much.
  3 commentaires
David Fletcher
David Fletcher le 11 Avr 2021
Modifié(e) : David Fletcher le 11 Avr 2021
Looking at it, I am slightly confused as to why you need the third dimension on the Var matrix. Surely, it only needs to be two dimensions. Saying that, the reason you are only getting three entries in your Var matrix, is because the i indexer is based on the length of the w vector (3), so there is no way on earth you can ever have more than three entries. You will need to change the way you index the answers into this matrix (maybe base it on a count that starts at one and increments every time you add something into it)
Something like this:
clear
w = [9 8 7];
x = [1 2 3];
y = [3 2 1];
count=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) <= 20
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(count,:) = [i j k]; % Vector with variables in order than produces solution
count=count+1;
end
end
end
end
This gives
Var =
1 1 2
1 1 3
1 2 3
2 1 2
2 1 3
2 2 3
3 1 2
3 1 3
3 2 3
Brandon Ratter
Brandon Ratter le 11 Avr 2021
In the more complex script I am using 5 variables so I made an Array based on all 5 variables, but now that you mention it, you are correct its far easier to have it as 2 dimensions. I've made the changes to both this script and the more lengthy one for my project and they both work well. Thanks for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by