comparing datasets of different sizes line row by row

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

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)
Thanks for the response, but matlab will not accept the x's as they do not stand for any number.
Error = "Subscript indices must either be real positive integers or logicals"
I tried having the Xs be all rows (1:end,3) and (2:end-1,3) but they cannot be compared to the A matrix since they are different sizes.

Sign in to comment.

Answers (1)

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

1 Comment

Sorry if I was unclear. Your response was different from my goal, but it was still helpful for generating ideas. The end goal of this section of code is to produce a matrix C with the same number of rows as matrix B but with the data of matrix A lined up with the data in B using column 3.
I have two sets of data imported from excel which I have defined here as A and B. The data in each set is different except for in column 3 data which I can use to match A to B (though they will not be equivalent, hence the < >) . B has many more rows of data than A so the A(x,3) data has to repeat while it is the value just below the corresponding value in B(x,3).
I think I am heading in the right direction in my code below, but I am still pretty lost. I hope this information makes things more clear and thanks again for the help.
S1 = size(B)
S2 = size(A)
for i = 1:S1 ;
for u = 1:S2;
if A(u,3)< B(i,3) && B(i,3) < A(u+1,3);
A(u,:)
elseif A(u,3)< B(i+1,3)&& B(i+1,3) < A(u+1,3);
A(u+1,:)
end
end
end

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 30 Jan 2016

Commented:

on 31 Jan 2016

Community Treasure Hunt

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

Start Hunting!