Asked by Thar
on 27 Feb 2015

Hi all!

I have two matrix:

First [A] has 2 columns and 4 lines:

107,407870375000 377,600000000000

107,415393500000 375,700000000000

107,498171291667 381,900000000000

107,556215291667 387,100000000000

Second [B] has 3 columns and 22 lines:

107,235567125000 343,885429687500 19,7753906250000

107,244513875000 377,138398437500 12,7882812500000

107,255046291667 377,455351562500 11,0281250000000

107,264641208333 382,923242187500 7,95039062500000

107,275659708333 385,504140625000 5,87578125000000

107,284224541667 385,393125000000 6,05664062500000

107,295682875000 394,200000000000 4,95234375000000

107,304583333333 389,552226562500 4,26210937500000

107,313958333333 389,240390625000 4,78945312500000

107,322245375000 387,623867187500 4,27656250000000

107,331585666667 385,900468750000 3,87921875000000

107,338703708333 389,454609375000 3,64628906250000

107,349247666667 387,801601562500 3,59406250000000

107,356365750000 388,281718750000 3,36320312500000

107,366388875000 384,638125000000 3,57777343750000

107,373182875000 390,168437500000 3,32972656250000

107,383472208333 382,523437500000 3,20972656250000

107,391018500000 382,931054687500 3,27320312500000

107,400300916667 383,220273437500 3,18894531250000

107,407222208333 379,468515625000 3,48558593750000

107,416759250000 386,672070312500 3,06492187500000

107,423726833333 381,518359375000 3,18691406250000

I want for each value from the matrix A and column 1 to find the nearest value from matrix B and column 1.

Then, I need to find mean value from the matrix B (columns 1,2 ) from the value i found previously +- 0.015.

For example, if i take the value 107.40787 from the [A], the nearest in [B] is 107,407222.

107.40722 +- 0.015 are the values from B:

107,400300916667 383,220273437500

107,407222208333 379,468515625000

107,416759250000 386,672070312500

Then, I will find means from these two columns and compare the values.

Thank you!!

Answer by Giorgos Papakonstantinou
on 27 Feb 2015

Edited by Giorgos Papakonstantinou
on 1 Mar 2015

Accepted Answer

Hallo Thodoris.

[~, ii] = min(abs(bsxfun(@minus,A(:, 1)',B(:,1)))); % These are the row indexes of B(:,1) which are nearest to each value of A(:,1)

B(ii,1) % for each element of A(:,1) these are the nearest values of B(:,1).

f = @(x, y) abs(x - y)<0.015; % create anonymous function to find which values fluctuate 0.015

idx = bsxfun(f, B(:,1), B(ii,1)');

Col1mean = ones(size(idx, 2), 1); % preallocate

Col2mean = ones(size(idx, 2), 1);

for ii = 1:size(idx, 2)

Col1mean(ii) = sum(B(:,1).*idx(:,ii), 1)./sum(idx(:,ii), 1);

Col2mean(ii) = sum(B(:,2).*idx(:,ii), 1)./sum(idx(:,ii), 1);

end

For the last part I have to cite the solution of Jan Simon from here. You can also find very good answers in this link on how to mean a matrix columnwise based on another logical matrix.

Thar
on 1 Mar 2015

idx = bsxfun(f, B(:,1), B(ii,1)')

What is B(ii,1)?

I have not designate the ii here.

Thank you!

Giorgos Papakonstantinou
on 1 Mar 2015

:-). I corrected my answer. In the first two commands I replaced idx with ii.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Thar (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/180747-how-compare-two-cplumns-from-different-matrix#comment_268917

## Giorgos Papakonstantinou (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/180747-how-compare-two-cplumns-from-different-matrix#comment_268930

## Thar (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/180747-how-compare-two-cplumns-from-different-matrix#comment_269040

Sign in to comment.