0

This is the same statement for 3 times. how to do this with loop. Here, the matrix G1 dimension is 3*10. so in each part i am taking different column.

G2 = f_range_m(1:8:length(timeline_ms),:); %%%% measured range
G1 = OrgiRange(1:8:end,:);

M1 = G1(:,1);
dist11 = abs(bsxfun( @minus,G2,M1));
[~,col1] = min(dist11,[],2);
Result_1 = diag(G2(:,col1));


M2 = G1(:, 2);
dist22 = abs(bsxfun(@minus,G2, M2));
[~,col2] = min(dist22,[],2);
Result_2 = diag(G2(:,col2));

M3 = G1(:, 3);
dist33 = abs(bsxfun(@minus,G2, M3));
[~,col3] = min(dist33,[],2);
Result_3 = diag(G2(:,col3));

I am trying this way. However not getting desired output. Result dimension should be 13*3.

obj = 3;
for ix = 1:obj
    M = G1(:,ix);
    dist = abs(bsxfun(@minus,G2,M));
    [~,col] = min (dist,[],2);
    Result = diag(G2(:,col));
end
3
  • Sorry, G1 is 13*3 and G2 is 13*50 Commented Feb 15, 2016 at 13:40
  • 3
    The problem with your code are the variable names you are using. You have to use arrays or cell arrays instead of variable names like M1 M2 M3. If you rewrite the code this way, going to a loop is straight forward. You just replace the M{1} with a M{ix} where ix is your loop iterator. Alternatively if you don't need M after the calculation just use M and overwrite it in each iteration. Do the same for dist, col and Result and you got a solution. Commented Feb 15, 2016 at 13:44
  • Yes, you are right. Thanks Daniel. Commented Feb 15, 2016 at 13:47

1 Answer 1

1

With your updated code you nearly solved it. Everything which remains is writing to the columns of Result instead of overwriting it in each iteration.

obj = 3;
Result=nan(size(G1,1),obj);
for ix = 1:obj
    M = G1(:,ix);
    dist = abs(bsxfun(@minus,G2,M));
    [~,col] = min (dist,[],2);
    Result(:,ix) = diag(G2(:,col));
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.