Sorting elements according to an array
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I will try my best to phrase this question, hope it makes sense
I have 2 arrays, DATA which has useful data and SCALE which has the scale where I am fitting the data to.
DATA has numbers from 1-100 in random order, and SCALE has distinct intervals of tens from 1-100, i.e. 1,10,20,30...90,100
My goal is the write a code which clumps the elements from DATA to SCALE to do calulations such as SCALE-DATA (for example, 36 is the DATA element, the SCALE element should be 40 and it would be 40-36)
example - 7 will be rounded up to 10, and 23 will be rounded down to 20.
I understand the commands ciel and floor, however I am not sure what is the most efficient way to match the rounded data to the scale?
採用された回答
Star Strider
2020 年 5 月 28 日
I am not certain what you want.
Try this:
DATA = randi([1 100], 25, 1); % Create DATA
SCALE = 10*round(DATA/10).*(DATA>=10) + 10*round(DATA/10).*(DATA<10);
Out = [DATA, SCALE];
.
10 件のコメント
Hi StarStrider, thanks as always for the reply.
This is very close to what I want, but my scale is already pre-defined. and I want to match the rounded ones to the SCALE.
Your code creates the SCALE vector according to the DATA.
SCALE DATA
10 24~20 (goes to the second column)
20 ...
30 ...
40 ...
50 ...
Appreciate the help, as always!
Star Strider
2020 年 5 月 29 日
My pleasure!
I am still not certain that I understand what you want, so I will give it another shot:
Out = sortrows([SCALE DATA],1)
the rest of the code is unchanged.
I do not understand this: 24~20 or what you want to do with it. It is not likely that it will work as a single column in MATLAB.
Hans123
2020 年 5 月 29 日
Thanks Star Strider, I am sorry for not being articulate enough. I believe in your code -
SCALE = 10*round(DATA/10).*(DATA>=10) + 10*round(DATA/10).*(DATA<10)
- makes the SCALE array according to the DATA array, however SCALE is predefined before the elements of DATA are introduced. Hope that helps sort it out.
Basically, DATA elements get rounded up or down to get close to an induvidual element of SCALE and gets sorted.
So there should be a line that says
SCALE = linspace(0,100,10);
Star Strider
2020 年 5 月 29 日
I am completely lost.
I will delete my Answer in a few minutes.
Oh wow, I am sorry Star Strider. Let me try to rephrase it again
Hans123
2020 年 5 月 29 日
Again, since I lost my command of the English language.
I have points scattered on the x axis, and I want the points to rounded up to the nearest division.

Your code does exactly what I want, however the scale is defined from the data input. And with different data sets the scale changes. My scale is predefined and is indepenndent of the data set.
SCALE = 10*round(DATA/10).*(DATA>=10) + 10*round(DATA/10).*(DATA<10);
Something along the lines of this, but this is what I can't figure out
%predefined
DATA = [8 17 21 23 36]';
SCALE = [10 20 30 40]';
rounddata = round(DATA);
%compare with SCALE to see where it belongs and output
Out = [correctDATA, SCALE];
Star Strider
2020 年 5 月 29 日
Try this:
DATA = randi([1 100], 1, 25); % Create ‘DATA’
SCALEfcn = @(x) 10*round(x/10).*(x>=10) + 10*round(x/10).*(x<10);
SCALE = linspace(0,100,11);
for k = 1:numel(SCALE)-1
Out{k,:} = [SCALE(k) DATA((SCALEfcn(DATA) >= SCALE(k)) & (SCALEfcn(DATA) < SCALE(k+1)))];
end
I still do not understand what you want. This sort of histogram approach is as close as I can get.
Experiment with it to get different results.
Hans123
2020 年 5 月 29 日
Thank you for your time, I really appreciate it. I can tinker around with the helpful lines you have given me. I am glad to hear from you during these uncertain times. As always, thank you for the help Star Strider. Always a blessing to have you in this forum!
Star Strider
2020 年 5 月 29 日
As always, my pleasure!
Thank you very much for your compliments and sentiments!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
参考
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)
