Creating new vectors containing values based on for loop outcome.
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I would like to create a new vector which contains values from b which correspond to the closest values from a. I mainly code in python and am new to Matlab. The only thing i can think of at the moment is an if statement which i know would most likely be inefficient. Essentially i would need to create new vectors for each value in a. The following is my code. Any help is greatly appreciated, thanks in advance.
a=[1250 2320 3520 7650];
b= [3700 6766 8888 1212 2000 5555 9998 3000];
vals=zeros(7,1);
n=1;
for i = b
differences = abs(a-i);
[minDiff, indexOfMinDiff] = min(differences);
closestValue = a(indexOfMinDiff);
vals(n)= closestValue;
n=n+1;
end
edges = unique(vals)
counts = histc(vals(:), edges)
採用された回答
vals=interp1(a,a,b,'nearest','extrap');
For above a,b:
>> vals
vals =
3520.00 7650.00 7650.00 1250.00 7650.00 7650.00 3520.00
>>
10 件のコメント
Michael Abram
2020 年 12 月 31 日
編集済み: Michael Abram
2020 年 12 月 31 日
Thank you, is this to find the closest value to the b values in a, if so is there a way of creating a vector with all values from a which correspond to the nearest b value?
So in this case there would be two vectors, for 3520 and 7650.
dpb
2020 年 12 月 31 日
Say what? Don't follow why "two vectors"...there are four values in a, why only two referred to?
I made a mistake in the comment, but ideally four vectors would be created, one for each value in a, and these vectors would contain the corresponding values from b.
K>> interp1(b,b,a,'nearest','extrap')
ans =
1212.00 3000.00 3700.00 6766.00
K>>
I don't know what four vectors you would want; the result is a 4-vector (=numel(a)) of the corresponding nearest value from b.
Creating four variables where one array will do is not "the MATLAB way" if that is the idea.
I need to find the mean value of all the b values closest to the same number in a, is there a way of doint this in MATLAB?
I don't understand what that means...the mean of the closest is just the closest value.
Illustrate what you think the answer should be and explain why. If you can clearly define the problem, of course MATLAB can be programmed to return the answer.
Michael Abram
2021 年 1 月 1 日
編集済み: Michael Abram
2021 年 1 月 1 日
If i have four numbers, 1000, 1200, 1400, 1600, and the closest number to all these in an alternate vector is 1500, i would like to find the mean of all these numbers. The code i am building is to perform k means clustering.
Don't try to be so terse -- this isn't Twitter.
Show what you would want the result to be from your original a,b and how this relates to the original Q? as asked and the desire for four elements.
It's all too disjointed to see the big picture of what you have and what you want.
You are aware there is a kmeans function in the MATLAB Statistics Toolbox, I presume...instead of reinventing the wheel?
Michael Abram
2021 年 1 月 1 日
編集済み: dpb
2021 年 1 月 2 日
I apologise, my description of what i wanted was extremely basic and i have finally managed to create the correct code. I apologise if i have wasted your time and am extremely thankful to you for taking the time to help me. The code to check the nearest value you gave me was extremely helpful. As you can see, the cent1 array contains the mean values of all the X values which share the same closest value in Incent1. I am aware of the kmeans function however i believe the problem i have been set involves creating my own kmeans function.
filename = 'Centroids.txt';
[inCent,delimiterOut]=importdata(filename);
filename = 'EPdata.txt';
[X,delimiterOut]=importdata(filename);
X1 = X.';
inCent1 = inCent.';
vals=interp1(inCent1,inCent1,X1,'nearest',"extrap");
n=1;
cent1=[];
for i = inCent1
k = find(vals==i);
l=length(k);
y=X(k);
s=sum(y)/l;
cent1(n)=s;
n=n+1;
end
cent1
filename = 'Centroids.txt';
[inCent,delimiterOut]=importdata(filename);
filename = 'EPdata.txt';
[X,delimiterOut]=importdata(filename);
vals=interp1(inCent,inCent,X,'nearest',"extrap");
n=0;
cent=zeros(numel(inCent),1);
for i = inCent.'
n=n+1;
cent(n)=mean(X(vals==i));
end
removing duplicated variables; only transposing where needed and eliminate unnecessary temporaries and the explicit find for the logical addressing vector.
Alernatively, can eliminate the explicit loop with
filename = 'Centroids.txt';
[inCent,delimiterOut]=importdata(filename);
filename = 'EPdata.txt';
[X,delimiterOut]=importdata(filename);
vals=interp1(inCent,inCent,X,'nearest',"extrap");
cent=arrayfun(@(i) mean(X(X==vals)),vals);
NB: air code, untested....
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
