Picking out a value from a Vector in a Loop iteration

2 ビュー (過去 30 日間)
Christopher
Christopher 2013 年 4 月 24 日
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
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
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
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

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by