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!