How do I check for already used elements?
Show older comments
Problem:
Starting with an Matrix of 2 columns. First column contains starting values, second column depending values. I want to search through 3 other matrices, all of different sizes, for values that are nearest to my starting values from matrix 1. Think of it like having a vector of deformations and the depending forces the second column.
What I want to avoid ist getting double values because I simply check for nearest values:
D = {a,b,c}
V1 = d
for k = 1:size(V1,1)
dist = abs(D{i}(:,2) - V1(k,1));
minDist = min(dist);
minIdx = (dist == minDist);
minIdx = find(minIdx,1,"first");
minVal = D{i}(minIdx);
end
If I do this, I will end up getting doubled values for different starting values.
Additional Info:
Before processing the code above I start identifying my longst matrix in terms of rows, then putting every second value from the deformation column into V1 and the corresponding froces into the second column.
This is about a mechanical observation and I got 4 measurements done. I can plot force-deformation-diagrams for each one and ultimately am trying to make a diagram of an averaged curve now.
I hope you can follow my asking and I am very sorry for my bad English. Ask me anything.
Best regards,
Jan
Answers (1)
David Hill
on 3 Apr 2022
D = {a,b,c};
v=d(:,1);
n=zeros(length(v),length(D));
for k=1:length(D)
[~,m]=min(abs(v-(D{k}(:,2))'),[],2);
n(:,k)=D{k}(m,2);%I believe this is the matrix you want
end
3 Comments
Jan Lorenz
on 3 Apr 2022
Image Analyst
on 3 Apr 2022
How can you tell if a value is "used" or not?
Perhaps one way would be to make an array of all nan's with the nan() function. Then assign elements. ANy remaining nan's will be "not used".
What kind of variables are a, b, and c? Why are you using cell arrays instead of regular numerical arrays?
Jan Lorenz
on 3 Apr 2022
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!