comparing datasets of different sizes line row by row
5 views (last 30 days)
Show older comments
So I have two matrices A and B which are different sizes from each other. I want the column data in B to be compared to that in A so that I can return the row in A that is less than B but the next row is greater than B.
Basically I need to use the values in a column of B to to select the correct row from matrix A.
What I think I want to do in non-matlab speak is what I have below.
for S = A(1:end,3)
if B(x,3) < A(x,3) < B(x+1,3)
B(x,3)
end
end
Thanks for any help you may provide.
2 Comments
per isakson
on 30 Jan 2016
If x is a scalar, replace
B(x,3) < A(x,3) < B(x+1,3)
by
B(x,3) < A(x,3) && A(x,3) < B(x+1,3)
Answers (1)
John BG
on 30 Jan 2016
Edited: John BG
on 30 Jan 2016
Try this
[LAr,LAc]=size(A)
[LBr,LBc]=size(B)
for i=1:1:LAc
col_A=A(:,i)
min_col_A=min(col_A)
max_col_A=max(col_A)
col_B=B(:,i)
max_col_B=max(col_B)
if max_col_B<min_col_A % checking if meeting B(x,3)<A(x,3)..
col2_B=B(:,i+1)
min_col2_B=min(col2_B)
if max_col_A<min_col2_B % checking if meeting A(x,3)<B(x+1,3)
% do whatever you have to
end
end
end
If you are image processing, A is a big image and B is a small block that has to run throughout the image, because the min max example is not exactly meeting the B(,j) < A(,j) < B(,j+1) then you need 2 for loops, but not sure if it's the case.
I was about to test it with these two:
A=randi(10,9,12) % 9 rows x 12 columns of uniform random integers within [1:10]
B=randi(10,3,3) % 7 rows x 4 columns of uniform random integers within [1:10]
But perhaps you want to either tell whether this answer helps you, by clicking on the thumbs-up and flag above, or would like to add more details so we can better understand the conditions to trigger B(,j) < A(,j) and A(,j) < B(,j+1)
Just in case, don't know in the non-Matlab language you use, but in MATLAB: [rows, columns] Hope it helps John
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!