43 views (last 30 days)

Greetings,

I want to check an (entire column) of B with a specific (row,column) in A and then extract values in B that are lower/higher than that value in A. For example; B(:,1) with A(1,1). I have already done this by creating a function file and calling it in my script.

My question now is how do I go from B(:,1) with A(1,1) to B(:,2) with A(2,2) without using this.

[Higher, Lower] = check(A(1,1), B(:,1))

[Higher1, Lower1] = check(A(1,2), B(:,2))

I want something that will automatically insert the desired matrix position. I'm asking this because my actual data set for matrices A = 30*12 and B = 62*12, thus making my method below inefficient. I have also included the expected results below.

clc; clear;

A = [1 2 1 2 5 2 1 2; 3 4 5 6 7 8 2 1];

B = [1 2;0 1; 2 4; 3 7; 0 5; 1 3; 2 4; 3 3; 9 8];

% Check B with A (I HAVE PROBLEM WITH THIS)

[Higher, Lower] = check(A(1,1), B(:,1))

[Higher1, Lower1] = check(A(1,2), B(:,2))

My function file:

function [Higher, Lower] = check(x,y)

% This function checks the y against x and extracts the respective higher/lower values.

% Set initial count to 0

con1 = 0;

count1 = 0;

for j = 1

for i = 1:9

if y(i) > x(j)

con1 = con1 + 1;

Higher(con1,j) = y(i);

end

end

con1 = 0;

end

for j = 1

for i = 1:9

if y(i) < x(j)

count1 = count1 + 1;

Lower(count1,j) = y(i);

end

end

count1 = 0;

end

end

Expected results:

Higher = [2;3;2;3;9]

Lower = [0;0]

Higher1 = [4;7;5;3;4;3;8]

Lower1 = [1]

All help is greatly appreciated. Thanks.

Jan
on 28 Mar 2019

Edited: Jan
on 28 Mar 2019

I've simplified the code at first:

A = [1 2 1 2 5 2 1 2; 3 4 5 6 7 8 2 1];

B = [1 2;0 1; 2 4; 3 7; 0 5; 1 3; 2 4; 3 3; 9 8];

[Higher, Lower] = check(A(1,1), B(:,1))

[Higher1, Lower1] = check(A(1,2), B(:,2))

function [Higher, Lower] = check(x,y)

% This function checks the y against x and extracts the respective higher/lower values.

Higher = y(y > x);

Lower = y(y < x);

end

This produces the wanted output. So what is the remaining problem? What does "how do I go from B(:,1) with A(1,1) to B(:,2) with A(2,2) without explicitly mentioning it" mean?

Maybe you want this:

A = rand(30, 12);

B = rand(62, 12);

Higher = cell(size(A));

Lower = cell(size(A));

for i1 = 1:size(A, 1)

for i2 = 1:size(A, 2)

[Higher{i1, i2}, Lower{i1, i2}] = check(A(i1, i2), B(:, i2));

end

end

I'm not sure, what the wanted output is. But for storing arrays of different sizes, cell arrays are perfect.

Jan
on 29 Mar 2019

@Elizabeth: This is not the complete message. The part, which mentions what the problem is, is missing.

I guess that now either A is the complete matrix, or B contains something other than mentioned before. Note that in all othercode before, check was called with 1 element of A only, A(i1,i2), but now you provide the complete matrix.

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/453082-checking-a-specific-column-in-a-matrix-with-a-specific-row-column-in-another-matrix#comment_687602

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/453082-checking-a-specific-column-in-a-matrix-with-a-specific-row-column-in-another-matrix#comment_687602

Sign in to comment.