MATLAB: Position of a value in matrix as (row,column)

matrix arraymatrix locationmatrix manipulation

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);

Best Answer

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
Related Question