Find all instances of a condition in x , replace corresponding y values with ymax (of subset or a give value) via for loop..data attached
1 回表示 (過去 30 日間)
古いコメントを表示
%% Trying to find id of repeted values in x, corresponding to h_w id, repalicng them with max (h_w) or given value,
% Following is my attept but no luck,
% because I want to skip x values already sorted by for loop in
% subsequent rounds
h_new linspace = (8.5,5.9, length (H_W));
% H_W (:) = 0;
for ii = 1: length (h_new)
[id, v] = find (x == x (ii));
H_W (id) = h_new (ii);
repeatedVal_h = H_W (id);
end
% The problem is loop, say once it locates all instances of x = 10, in x and has replaced in h_w,
% it must ignore, next x = 10 in for loop.
% I hope I made clear
1 件のコメント
Bob Thompson
2019 年 2 月 13 日
So let me rephrase this and see if I understand.
You want to identify all instances of a value in x and replace the corresponding values of H_W with the index loop of h_new.
This part of the code is working, but the problem is that for repeating values of x the code does not skip the later instances of the same value, causing the values of H_W to change multiple times.
Does that about right?
採用された回答
BlueBird
2019 年 2 月 13 日
編集済み: BlueBird
2019 年 2 月 13 日
2 件のコメント
Bob Thompson
2019 年 2 月 13 日
What I'm getting from this is that you want to replace all of the indices of x which correspond to a certain value (say 2) with a desired value (say 100). My code will do this, you just need to change what h_new(hcount) values are. Something like this.
uniques = unique(x);
hcount = 1;
for ii = 1:length(uniques);
[id, ~] = find(x == uniques(ii));
H_W(id) = 100*hcount;
repeatedVal_h = 100*hcount;
hcount = hcount + 1;
end
I don't expect that to do exactly what you're looking for, I'm just giving an example of how you can change your desired value within my sample I provided.
その他の回答 (2 件)
Bob Thompson
2019 年 2 月 13 日
Instead of looping through all values of h_new, you might try looping through all unique values of x.
uniques = unique(x);
hcount = 1;
for ii = 1:length(uniques);
[id, ~] = find(x == uniques(ii));
H_W(id) = h_new(hcount);
repeatedVal_h = h_new(hcount);
hcount = hcount + 1;
end
Without seeing your data I cannot confirm, but I suspect this will drastically reduce the number of h_new values you progress through, but should eliminate looping over duplicates.
0 件のコメント
参考
カテゴリ
Help Center および 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!