Picking out a value from a Vector in a Loop iteration
2 次查看(过去 30 天)
显示 更早的评论
What i would like to do is from a vector = [a b c d], obtained from calculating the distance between 2 vectors, i want in my iteration to be able to choose lets say c if c is the least distance away. I am trying to implement LBG design algorithm for data compression. This is what i have so far clear all close all
x1 = [8.4 1.9 0.2 6.8 6.8 3.0 3.8 5.4 8.3 1.5 5.1 7.0 7.1 3.8 4.3 8.6 3.0 8.5 1.9 5.9]'; y1 = [2.5 2.5 2.5 7.5 7.5 2.5 7.5 7.5]'; X = reshape(x1,2,10)'; Y = reshape(y1,2,4)';
figure(1) plot(X(:,1),X(:,2),'*');xlabel('X1');ylabel('X2'); hold on plot(Y(:,1),Y(:,2),'o');
for i = 1:length(X) for m =1:length(Y) d = X(i,:)-Y(m,:); x(i,m) = sqrt(sum(d.^2)) end end
x will spit out a 10 by 4 matrix. I want to pick out the least distant value in the for loop to get a 10 by 1 matrix
2 个评论
Matt Kindig
2013-4-24
If I understand you correctly, you just want the smallest value for each row. After your two for loops, just add
smallestRow = min(x, [], 2);
Matt Kindig
2013-4-24
By the way, the delay in you getting a response was because your code was not formatted correctly. In the future, please use the '{} Code' button above to format your code. After copy-pasting your code into the question window, highlight it and click the Code button. This will make it easier for forum members to understand your problem more easily.
回答(1 个)
Ahmed A. Selman
2013-4-24
Within your same code:
...
plot(Y(:,1),Y(:,2),'o');
z=zeros(1,length(X)); % Add this
for i = 1:length(X)
for m =1:length(Y)
d = X(i,:)-Y(m,:);
x(i,m) = sqrt(sum(d.^2));
end
z(i)=min(min(x(i,:))); % Add that
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!