How to increment a vector
57 次查看(过去 30 天)
显示 更早的评论
I'm doing a college project on filters. A filter is dependent on two vector properties - A and B. Both look similar to [0.2345 0.2314 0.8290 0.000]. What I want to do is, I want to start off with both being [0 0 0 0] and increment A in steps of 0.001. This requires me being able to increment the last component, then when it reaches 0.999, on the next increment it will go to zero and A = [0 0 1 0].
Then when vector A = [0.999 0.999 0.999 0.999], a for loop should be able to increment vector B in the same way, thus calculating every possible value for A and B assuming they both have four components.
Any help is appreciated guys
1 个评论
sixwwwwww
2013-12-6
when you increment last element of A with 0.001 and reach at 0.999 then you get A = [0 0 1 0] but how then you will go back to A = [0.999 0.999 0.999 0.999]? because 1 is greater than 0.999 so you also need some decrement?
采纳的回答
sixwwwwww
2013-12-7
编辑:sixwwwwww
2013-12-7
you can do it in a loop as follows:
A = [0 0 0 0];
B = [0 0 0 0];
lim = 0.999;
inc = 0.001;
count = 1;
tic
while B(1) < lim
if B(2) >= lim
B(1) = B(1) + inc;
B(2) = 0;
elseif B(3) >= lim
B(2) = B(2) + inc;
B(3) = 0;
elseif B(4) >= lim
B(3) = B(3) + inc;
B(4) = 0;
elseif A(1) >= lim
B(4) = B(4) + inc;
A(1) = 0;
elseif A(2) >= lim
A(1) = A(1) + inc;
A(2) = 0;
elseif A(3) >= lim
A(2) = A(2) + inc;
A(3) = 0;
elseif A(4) >= lim
A(3) = A(3) + inc;
A(4) = 0;
else
A(4) = A(4) + inc;
end
count = count + 1;
AandB = [B A];
end
But be careful!!!
In order to get from A = [0 0 0 0] and B = [0 0 0 0] to A = [0.999 0.999 0.999 0.999] and B = [0.999 0.999 0.999 0.999] in increments of 0.001, the loop will run for almost 10^24 times. So its your choice whether you want to do it?
Just for information. I estimated and it will take about 3 * 10^15 years on my laptop. So don't try to do it even if you have supercomputer it will take more than a lifetime
0 个评论
更多回答(1 个)
Walter Roberson
2013-12-7
To confirm: you want A(4) to go through 1000 different states before incrementing A(3), and A(3) has to go through 1000 different states before incrementing A(2), and so on, and only when all of the A are in the maximum state, should B(4) be incremented? And this is to continue on in the same pattern, with B(3) not incremented until it is time to increment B(4) beyond its last state?
If that is correct, then you would be putting A through 1000^4 = 10^12 different states for each increment of B, and with there being 1000^4 = 10^12 different states for B, a complete run would require 10^12 * 10^12 = 10^24 total states. That is more than 2^79 different states run through. There is no known computer or cluster that can run through that many different states within the lifetime of the Universe.
2 个评论
Walter Roberson
2013-12-7
Consider using ndgrid() and linspace() together:
A = cell(4,1);
B = cell(4,1);
NS = 16+1; %16 internal divisions plus 1 final endpoint
[A{1} A{2} A{3} A{4} B{1} B{2} B{3} B{4}] = ndgrid( linspace(0, 1, NS) );
Then where your existing function references A(1) reference A{1} instead, and so on. Provided that your formula is sufficiently vectorizable, you can then calculate all of the answers "at the same time". For whatever good that will do you.
Are you trying to do something like find a minima or maxima? If so then consider using one of the minimization tools such as fmincon()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 ANOVA 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!