Incrementing rows and columns through looping
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2016-10-13
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.
回答(1 个)
KSSV
2016-10-13
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!