Using a while loop on each column in an array?

6 views (last 30 days)
Tyler
Tyler on 5 May 2016
Commented: Tyler on 5 May 2016
Hi all,
I have a script that performs an action on each column of an array at once. I would like to set up a loop where I do this multiple times. Each time I will be taking the mean of each column and comparing it to another value. I would like to know at which iteration each column's mean reaches that value, although each column will do this at different times. With each iteration the columns will get 1 longer, thus making the mean more accurate.
I don't want my script to stop when only one of the columns reaches this point, but I also don't want it to stop and only record the iteration when all of them have reached this value.
For instance:
B = 4;
for i = 1:4
if i == 1
A = [1,2,3;1,2,3;1,2,3;4,4,4];
elseif i == 2
A = [1,2,3;1,2,3;1,2,3;4,4,4;4,4,4];
elseif i == 3
A = [1,2,3;1,2,3;1,2,3;4,4,4;4,4,4;4,4,4];
elseif i == 4
A = [1,2,3;1,2,3;1,2,3;4,4,4;4,4,4;4,4,4;4,4,4];
end
j = mean(A)/B;
end
If I want j > .65, I would like a vector that tells me column one reached this at i == 4, column 2 reached this at i == 2 and column 3 reached this at i == 1. If I didn't use a while loop I do have a fixed number of iterations I could do, I would just like to avoid doing them all if I can help it. But maybe I could do that and then go back with cumsum? Any help would be appreciated, thank you!
  2 Comments
CS Researcher
CS Researcher on 5 May 2016
First of all mean(A)/B will be a vector and not a scalar value. Also, I am not sure I completely understood your problem. Can you elaborate your example further?
Tyler
Tyler on 5 May 2016
Yes, sorry for the confusion. You're right, j will be a vector with the mean of each column. I would like to know when each column of j is greater than .65 for example. With each loop my output array A gets bigger(in this example I entered in A manually, but in my script it's more complicated), and my j vector changes each time. However, my problem is that I would like to do this for more than 4 iterations- more like 260 at the most. But I would like it to stop when all the columns have reached that value. I would also like to know when each of the other columns reached above .65.
So for this example,
j(1) = [.4375 .6250 .8125]
j(2) = [.5500 .7000 .8500]
j(3) = [.6250 .7500 .8750]
j(4) = [.6786 .7857 .8929]
I would like an output vector with values of:
[4 2 1]
where column 1 was > .65 when i == 4, column 2 reached > .65 when i == 2, and column 3 was > .65 when i == 1.
Also, if I didn't know how many iterations it would take, I would like to set up the loop with
for i = 1:100
blah blah blah
end
But then I would like it to stop at i == 4 and not continue. Does this make it clearer? Sorry it's taking so long and proving difficult to explain my problem- thanks for your time!

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 5 May 2016
I'm not entirely clear on everything particularly on the stop condition for your loop.
You can indeed calculate the result a posteriori:
A = [1,2,3;1,2,3;1,2,3;4,4,4;4,4,4;4,4,4;4,4,4];
cummean = bsxfun(@rdivide, cumsum(A), (1:size(A, 1))');
%we can use max to find the first row of each column above a threshold or use a loop
[~, firstrow] = max(cummean / 4 > 0.65, [], 1)
firstrow = firstrow - 4
If you want to do the tracking in the loop, it's also trivial to do:
fullA = [1,2,3;1,2,3;1,2,3;4,4,4;4,4,4;4,4,4;4,4,4];
firstrow = zeros(1, size(A, 2));
for iter = 1 : size(fullA, 1)
A = fullA(1:iter, :);
abovethreshold = mean(A) / 4 > 0.65;
firstrow(abovethreshold & ~firstrow) = iter;
if all(firstrow)
break; %is this your stop condition?
end
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!