How can I find maximum value within each columns and and return the entire elements in that column

2 views (last 30 days)
Hello,
Here it is my question:
I have follwoing 3*8 matrix:
A =
0.0596 0.0714 0.8181 0.1499 0.9730 0.4538 0.0835 0.3909
0.6820 0.5216 0.8175 0.6596 0.6490 0.4324 0.1332 0.8314
0.0424 0.0967 0.7224 0.5186 0.8003 0.8253 0.1734 0.8034
What I want is finding maximum value of each two consecutive column and returning entire element of that column which includes maximum value. I also must mention that I want to compare two consecutive together and not to rest of columns. For example, I wont compare second column with third one.
Considering my description ad question following matrix B (3*4) would be the answer.
B =
0.0596 0.8181 0.9730 0.3909
0.6820 0.8175 0.6490 0.8314
0.0424 0.7224 0.8003 0.8034
Thank you so much in advance!

Accepted Answer

Hernia Baby
Hernia Baby on 27 Feb 2021
Edited: Hernia Baby on 27 Feb 2021
This code seperates A to odd and even elements, and culculates B with logical indexing.
A = [ 0.0596 0.0714 0.8181 0.1499 0.9730 0.4538 0.0835 0.3909;
0.6820 0.5216 0.8175 0.6596 0.6490 0.4324 0.1332 0.8314;
0.0424 0.0967 0.7224 0.5186 0.8003 0.8253 0.1734 0.8034];
A_odd = A(:,1:2:end-1);
A_even =A(:,2:2:end);
B = A_odd .* (A_odd > A_even) +A_even .* (A_even > A_odd)
B =
0.0714 0.8181 0.9730 0.3909
0.6820 0.8175 0.6490 0.8314
0.0967 0.7224 0.8253 0.8034
  6 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!