Incrementing rows and columns through looping

조회 수: 2 (최근 30일)
Raahul Palanivel Uma
Raahul Palanivel Uma 2016년 10월 13일
댓글: Adam 2016년 10월 13일
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
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
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by