Position of a value in matrix as (row,column)
2 次查看(过去 30 天)
显示 更早的评论
i just learn matlab. can you fix this coding to make a1_val=SNR(a1_loc); have same value.
clc;
clear;
close all;
SNR=[1,4,5,9;2,0,4,5;5,1,3,2;6,7,3,3];
a=max (SNR(1,:));
b=max (SNR(2,:));
c=max (SNR(3,:));
d=max (SNR(4,:));
a1=max (SNR(:,1));
b1=max (SNR(:,2));
c1=max (SNR(:,3));
d1=max (SNR(:,4));
[barisA,kolomA]=find(SNR==a1); %pr
[barisB,kolomB]=find(SNR==b1);
[barisC,kolomC]=find(SNR==c1);
[barisD,kolomD]=find(SNR==d1);
a1_loc=[barisA,kolomA];%PR
b1_loc=[barisB,kolomB];
c1_loc=[barisC,kolomC];
d1_loc=[barisD,kolomD];
x=SNR(1,1);
y=SNR(3,4);
a1_val=SNR(a1_loc);
b1_val=SNR(b1_loc);
c1_val=SNR(c1_loc);
d1_val=SNR(c1_loc);
1 个评论
Ameer Hamza
2020-3-20
The question is not clear.
a1_val=SNR(a1_loc)
has an assignment operator. Both side will have same value after execution.
采纳的回答
Sriram Tadavarty
2020-3-20
Hi Diah,
To get the value of a1_val from the SNR, you could try the following:
% Option 1: Without any find and then accessing it within the matrix
a1_val=SNR(SNR==a1);
b1_val=SNR(SNR==b1);
c1_val=SNR(SNR==c1);
d1_val=SNR(SNR==d1);
% Option 2: Converting the matrix into a vector and then accessing in the matrix
barisA=find(SNR(:)==a1);
barisB=find(SNR(:)==b1);
barisC=find(SNR(:)==c1);
barisD=find(SNR(:)==d1);
a1_val=SNR(barisA);
b1_val=SNR(barisB);
c1_val=SNR(barisC);
d1_val=SNR(barisD);
% Option 3: To use a for loop to access each element of row and column, with find
[barisA,kolomA]=find(SNR==a1);
[barisB,kolomB]=find(SNR==b1);
[barisC,kolomC]=find(SNR==c1);
[barisD,kolomD]=find(SNR==d1);
a1_loc=[barisA,kolomA];
b1_loc=[barisB,kolomB];
c1_loc=[barisC,kolomC];
d1_loc=[barisD,kolomD];
a1_val = [];
b1_val = [];
c1_val = [];
d1_val = []
for i = 1:size(a1_loc)
a1_val = [a1_val;SNR(a1_loc(i,1),a1_loc(i,2))];
end
% Repeat the above loop for variables b1_val, c1_val adn d1_val
The output is not as you expected is because of the indexing rules in MATLAB. For more details on indexing variables in matrices, look here.
Hope this helps.
Regards,
Sriram
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!