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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
BlueBird
am 13 Feb. 2019
Bearbeitet: BlueBird
am 14 Feb. 2019
%% 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 Kommentar
Bob Thompson
am 13 Feb. 2019
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?
Akzeptierte Antwort
BlueBird
am 13 Feb. 2019
Bearbeitet: BlueBird
am 13 Feb. 2019
2 Kommentare
Bob Thompson
am 13 Feb. 2019
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.
Weitere Antworten (2)
Bob Thompson
am 13 Feb. 2019
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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!