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 个评论
Rik
2022-7-20
It sounds like you have the steps clear. What did you try to implement them?
Miguel Albuquerque
2022-7-20
编辑:Miguel Albuquerque
2022-7-20
dpb
2022-7-20
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.
Miguel Albuquerque
2022-7-20
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.
Miguel Albuquerque
2022-7-20
dpb
2022-7-20
That does what you asked for...I'm not trying to decipher/debug yours...
Miguel Albuquerque
2022-7-20
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!