How do I check for already used elements?
이전 댓글 표시
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
답변 (1개)
David Hill
2022년 4월 3일
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
Jan Lorenz
2022년 4월 3일
Image Analyst
2022년 4월 3일
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
2022년 4월 3일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!