Incrementing rows and columns through looping

Hello
I'm using a for Loop to determine the closest values for a given Array and store the results in a matrix. The dimensions of the given arrays are A[2001x81] and B[733x1]. The command looks like this
for p = 1:size(A,2)
for q = 2:size(B,1)
count = count + 1;
aa1(:,count) = max(A(A(:,p)<B(q,:)));
end
end
When I excute the Loop, the values are stored in a single vector [1x59373]. How do I execute the Loop to store the values for each row and column in the same Matrix which should be {733x81] in the end? Thanks in advance.

1 Comment

aa1(:,count) = max(A(A(:,p)<B(q,:)))';
probably works though I don't have your data to test it on. I imagine there are also ways to do this without a nested for loop, but I don't have time to consider them at the moment.

Sign in to comment.

Answers (1)

A = rand(2001,81) ;
B = rand(733,1) ;
count = 0 ;
aa1 = zeros(81,733) ;
for p = 1:size(A,2)
for q = 2:size(B,1)
temp = max(A(A(:,p)<B(q)));
if ~isempty(temp)
aa1(p,q) = temp ;
else
aa1(p,q) = NaN ;
end
end
end

Categories

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

Asked:

on 13 Oct 2016

Commented:

on 13 Oct 2016

Community Treasure Hunt

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

Start Hunting!