Replace values in a array

Hey to all, thanks in advance
Is there a way of: for each row of distance_matrix, for the values that are not zero,find the most aproximate values in the array range with those of that row in distance_matrix. Once we identified in which column the values are most similar to those in the row of distance_matrix, replace those values in range by the values present in each row of RMC2.
Thank you

8 Comments

It sounds like you have the steps clear. What did you try to implement them?
distance_matrix=round(distance_matrix);
range=round(range);
RMC2=round(RMC2);
for i = 1:size(RMC2,1)
thisrow=RMC2(i,:);
for ii= 1:size(distance_matrix,1);
thisrow2=distance_matrix(ii,:);
isR2=find(thisrow2);
thisrow2=distance_matrix(ii,isR2);
[val,pos]=intersect(range,thisrow2)
pos=pos.';
first_element=pos(1,1);
last_element=pos(1,end);
value=(last_element-first_element)+1;
thisrow=RMC2(i,(1:value))
range(:,(first_element:last_element))=thisrow;
end
end
I did this, and is working fine, just an error, that its replacing the first iteration, so in column of range(:,253:272) it should be 95, and in 2nd iteration from range(:,272:341) it should be 102. But my code is replacing the first iteration by the second, meaning range(:,253:272)=102 and range(:,272:341)=102, and I dont want that,Can you help me pls?
Still most confusing what you're actually trying to do here -- if I look at the arrays, I find the following --
>> whos distance_matrix range RMC
Name Size Bytes Class Attributes
RMC 2x400 6400 double
distance_matrix 2x400 6400 double
range 1x1600 12800 double
>>
but that range is just a fixed delta from 0 so it could be computed on the go.
Figuring out what is inside the other two (got tired typing so named distance, 'd')--
>> sum(d~=0,2)
ans =
93
93
>> sum(RMC~=0,2)
ans =
93
93
>>
both have 93 elements that aren't zero in each row -- if you want the new RMC values there to go into distance, why not just assign them from the starting indices of each?
The Q? asked about nearest leads to a conundrum -- since 100 elements are too many to observe easily, I just picked first 10 out of the location of d as
ix=find(d(1,:)); % the first row non zero distances
ir=interp1(range,1:numel(range),d(1,ix(1:10)),'nearest'); % nearest element in range first 10
>> ir
ir =
254 255 255 256 256 256 257 257 258 258
>> range(ir) % what are those range values?
ans =
94.8750 95.2500 95.2500 95.6250 95.6250 95.6250 96.0000 96.0000 96.3750 96.3750
>> d(1,ix(1:10)) % compare to actual distances...
ans =
94.9718 95.1356 95.3010 95.4681 95.6369 95.8074 95.9797 96.1538 96.3297 96.5074
>>
So, now what? The nearest range to the distance isn't unique -- so which RMC to use -- the duplicates from the ir indices that are nearest? If so, I'm still at a loss as to why you don't just calculate the values for the given distances.
The previous answers re: using logical addressing still hold, only your RMC vector isn't matching because it is full of irrelevant information that I had presumed wouldn't be there is all.
This is a result from a very complex processing of radar.
Basically I use distance_matrix as the estimated parameters.
RMC2 is a correction to the vector range, given by distance_matrix,
Range- first vector
RMC2- Correction to that vector.
Distance_matrix- Estimated values.
The code I made just gives me this, but im doing an error I cant figure out where it is, because its replacing the 2row of RMC2 by the first row.
So at the end I should have range corrected, and I m getting that but first iteration is being replaced by second one, how can I solve that?
dpb
dpb on 20 Jul 2022
Edited: dpb on 20 Jul 2022
Your Q? are as varying as the radar...they keep moving target.
If you want the nearest value of the RMC vector, then use
for i=size(d,2)
ir=interp1(range,1:numel(range),d(i,:)),'nearest');
range(i,range(i,:)~=0)=RMC(i,ir);
end
otherwise it's just
for i=size(d,1)
range(i,range(i,:)~=0)=RMC(i,find(RMC(i,:));
end
Your above code uses : for row indexing that addresses both rows.
That could/would be ok if the result of ir were the same for both rows and you addressed both rows of RMC and they also overlap.
Sorry I didnt understand how can I change that in my code, is not there a way I could correct what you said in my code,
That does what you asked for...I'm not trying to decipher/debug yours...
done
distance_matrix=round(distance_matrix);
range=round(range);
RMC2=round(RMC2);
for i =1:size(RMC2,1)
thisrow=RMC2(i,:)
thisrow2=distance_matrix(i,:)
isR2=find(thisrow2)
thisrow2=distance_matrix(i,isR2)
[val,pos]=intersect(range,thisrow2)
pos=pos.';
first_element=pos(1,1)
last_element=pos(1,end)
value=(last_element-first_element)+1
thisrow=RMC2(i,(1:value))
range(1,(first_element:last_element))=thisrow
end

Sign in to comment.

Answers (0)

Products

Release

R2021b

Tags

Asked:

on 20 Jul 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!