MATLAB Answers


Find all instances of a condition in x , replace corresponding y values with ymax (of subset or a give value) via for attached

Asked by BlueBird on 13 Feb 2019
Latest activity Edited by BlueBird on 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);
% 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 Comment

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?

Sign in to comment.

3 Answers

Answer by BlueBird on 13 Feb 2019
Edited by BlueBird on 13 Feb 2019
 Accepted Answer

Hi Bob! I have attached the data (see attachment), I want only to replace value corresponding to non unique ones, e.g. look at a the short example in the table .


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;
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.
.Thank you it solve my probelm for now.
Yes, it does exactly what I want, very slow though.

Sign in to comment.

Answer by BlueBird on 13 Feb 2019
Edited by BlueBird on 13 Feb 2019

Thanx Bob for responding to a SOS signal:
Yes, Exactly, Let me make it more clear:
What I wnat to do is :
locate all the instance of say x =10; find indices , then replace these indices in h_w , replace them with a desired value, which may be single value or some linearly decreasing etc.
but the loop must go on and find indices of x=11, replace, corresponding value with next next desired value:
The length of vectors must remain intact, like you cant delete repating values etc.
x h_w desired
____ ______ ____
2 10 100
2 10.077 100
4 10.154 400
4 10.231 400
3 10.308 300
2 10.385 100
3 10.462 300
4 10.538 400
5 10.615 500
3 10.692 300
2 10.769 100
4 10.846 400
6 10.923 600
2 11 100


Sign in to comment.

Answer by Bob Nbob
on 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;
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.


Sign in to comment.