Store variables from a loop to use later
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
David Fletcher
2021-4-11
编辑:David Fletcher
2021-4-11
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 个评论
David Fletcher
2021-4-11
编辑:David Fletcher
2021-4-11
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!