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

2 Ansichten (letzte 30 Tage)
diah Lestari
diah Lestari am 20 Mär. 2020
Kommentiert: diah Lestari am 21 Mär. 2020
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 Kommentar
Ameer Hamza
Ameer Hamza am 20 Mär. 2020
The question is not clear.
a1_val=SNR(a1_loc)
has an assignment operator. Both side will have same value after execution.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sriram Tadavarty
Sriram Tadavarty am 20 Mär. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by