Suppose I have 3 column matrices or 4*3 matrix stored in A such that
A = [1 3 5 ; 2 4 5 ; 5 4 7 ; 2 8 10]
and i have another row matrix with 3 columns(no. of columns same as A) stored in B such that
B = [29 87 15]
Now i want to display the least value in B and corresponding column vector in A.
Expected Output:
since from the above 15 is least in B set , i will display 15 along with corresponding column in A(15 is in 3rd column so we take 3rd column of A) such that
C = 15 (least value in B)
D = [5 ; 5 ; 7 ; 10] (corresponding 3rd column of least value)
I need a generalised code suppose if i have A = (15*150 matrix) and B = (1*150 matrix) then i need to display the least values in B and as well as display the corresponding column(same column where least value present in B) in A. I hope this explanation helps, thank you in advance

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2023 年 6 月 22 日
編集済み: KALYAN ACHARJYA 2023 年 6 月 22 日

0 投票

A = [1 3 5 ; 2 4 5 ; 5 4 7 ; 2 8 10]
A = 4×3
1 3 5 2 4 5 5 4 7 2 8 10
B = [29 87 15]
B = 1×3
29 87 15
idx=find(min(B)==B)
idx = 3
data=A(:,idx)
data = 4×1
5 5 7 10
It can be condensed into a single line as well. You can now proceed with the remaining parts on your end too.

9 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 6 月 22 日
A = [1 3 5; 2 4 5; 5 4 7; 2 8 10];
B = [29 87 15];
%Another approach without find()
[~,idx]=min(B)
idx = 3
data=A(:,idx)
data = 4×1
5 5 7 10
barath manoharan
barath manoharan 2023 年 6 月 22 日
編集済み: barath manoharan 2023 年 6 月 22 日
thank you for your response @KALYAN ACHARJYA @Dyuman Joshi ,if there is more than one minimum values in B then how to fetch the corresponding column sir.
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 22 日
Which values do you want if there are more than one minimum values? 1st minimum, 2nd or last? Or All?
KALYAN ACHARJYA
KALYAN ACHARJYA 2023 年 6 月 22 日
A = [1 3 5; 2 4 5; 5 4 7; 2 8 10];
B = [29 15 15]; % here two minimum numbers
[~,idx]=find(B==min(B));
data=A(:,idx)
data = 4×2
3 5 4 5 4 7 8 10
barath manoharan
barath manoharan 2023 年 6 月 22 日
thank you for your response @Dyuman Joshi , if there is more than one minimum values (similar) then i need to display the 1st minimum value in B and its corresponding column in A
barath manoharan
barath manoharan 2023 年 6 月 22 日
Thank you for your response @KALYAN ACHARJYA
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 22 日
Then proceed with the approach I mentioned -
A = [1 3 5; 2 4 5; 5 4 7; 2 8 10; 3 5 7];
B = [29 87 15 15];
[~,idx]=min(B)
idx = 3
data=A(:,idx)
data = 5×1
5 5 7 10 7
Stephen23
Stephen23 2023 年 6 月 22 日
編集済み: Stephen23 2023 年 6 月 22 日
Regarding https://www.mathworks.com/matlabcentral/answers/1986584-display-corresponding-column-matrix#comment_2791224 even for multiple minimums, no FIND is required, logical indexing is simpler and more efficient:
A = [1,3,5; 2,4,5; 5,4,7; 2,8,10];
B = [29 15 15]; % here two minimum numbers
idx = B==min(B);
out = A(:,idx)
out = 4×2
3 5 4 5 4 7 8 10
barath manoharan
barath manoharan 2023 年 6 月 22 日
thank you for your responses @Dyuman Joshi @KALYAN ACHARJYA @Stephen23

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by