comparing two matrices of different dimensions

i have two matrices,A with dimension (23,2) and B with dimension (50465,2) .
i compare the first col of each matrix.
i need to keep values such that : abs(A(:,1)-B(:,1)) < 0.5.
C has four col : A(:,1) B(:,1) A(:,2) B(:,2)
my trial :
num_rows1=size(A(:,1));
num_rows2=size(B(:,1));
for i=1:1:num_rows1
for j=1:1:num_rows2
if abs(A(j,1)-B(i,1))<=0.5
%if data1(i,1)==0
% data1(i,1)=[];
% end
c1(j,[1:4])=[A(i,1),B(j,1), A(j,2), B(i,2)] ;
end
end
end
++++
ANY HELP

댓글 수: 2

"ANY HELP"
Why? for what?
c1(j,[1:4])=[A(i,1),B(j,1), A(j,2), B(i,2)] ;
You take an element from row #j (and 2nd column) of A (third element of rhs) and j supposes to be up to 50465?
What did you said? A has 23 rows?

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2023년 8월 21일
편집: Bruno Luong 2023년 8월 21일
Fix several bugs of your for-loop code
load('data1.mat');
load('data2.mat');
A=data1;
B=data2;
num_rows1=size(A(:,1),1); % BUG here
num_rows2=size(B(:,1),1); % BUG here
c1 = nan([num_rows2,4]);
for i=1:1:num_rows1
for j=1:1:num_rows2
if abs(A(i,1)-B(j,1))<=0.5 % BUG here
c1(j,[1:4])=[A(i,1),B(j,1),A(i,2),B(j,2)] ; % BUG here
end
end
end
c1
c1 = 50465×4
0.1050 0.0083 1.5745 2.3700 0.1050 0.0167 1.5745 2.3600 0.5222 0.0250 2.0570 2.3500 0.5222 0.0333 2.0570 2.3400 0.5222 0.0417 2.0570 2.3100 0.5222 0.0500 2.0570 2.3300 0.5222 0.0583 2.0570 2.3000 0.5222 0.0667 2.0570 2.2700 0.5222 0.0750 2.0570 2.2700 0.5222 0.0833 2.0570 2.3000

댓글 수: 4

Bruno Luong : thanks for your response.
please, i Need to resort c1 acorrding to the second col. ????
Bruno Luong
Bruno Luong 2023년 8월 21일
편집: Bruno Luong 2023년 8월 21일
Take a look at sortrows command
c1sorted = sortrows(c1, 2)
Bruno Luong : yes. it works..
if you please.. i need to classify c1sorted to be hourly-based data.
for example:
for 0 < col2 <=1 get the median of corresponding col3 (and col4)
for 1 < col2 <=2 get the median of corresponding col3 (and col4)
.
.
.
.
for 23 < col2 <=24 get the median of corresponding col3 (and col4)
so, i get a matrix of three columns:
c1final= [i median(col3) median(col4)];
where i =1:24
Separate question needs separate thread

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 8월 20일

0 개 추천

Here is an alternate way using pdist2 in the Statistics and Machine Learning Toolbox.
% Create simple, sample integer data.
A = randi(9, 15, 2) % [x, y]
B = randi(9, 25, 2)
% Find distances between each each point and every other point.
distances = pdist2(A, B)
% Find map of where distances are less than some threshold
threshold = 3; % Whatever closeness value you want.
closeDistances = distances <= threshold
[rowsOfA, rowOfB] = find(closeDistances)
% Get in form of xA, xB, yA, yB
C = [A(rowsOfA, 1), B(rowOfB, 1), A(rowsOfA, 2), B(rowOfB, 2)]

댓글 수: 2

Image Analyst : Thanks for kind attention.
unfortunately, i havnt install Machine Learning Toolbox.
So, " for loop "is perferable.
Then we're not sure what you're asking when you tersely say "Any help". You have a for loop, which seems to be your "preferable" way. So what's the problem?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Sparse Matrices에 대해 자세히 알아보기

질문:

2023년 8월 20일

댓글:

2023년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by