How to increment a vector

조회 수: 54 (최근 30일)
Evan
Evan 2013년 12월 6일
댓글: Walter Roberson 2013년 12월 7일
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
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
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

추가 답변 (1개)

Walter Roberson
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
Evan
Evan 2013년 12월 7일
Challenge accepted.
...okay, i'll reduce my expectations...
Walter Roberson
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 CenterFile Exchange에서 ANOVA에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by