How can I do matrix indexing

4 ビュー (過去 30 日間)
Ashraf Sherif
Ashraf Sherif 2019 年 2 月 8 日
コメント済み: Ashraf Sherif 2019 年 2 月 12 日
I want to select 3 elements from the matrix C, one element from each column, the selecting will be regarding to the maximum value in in each column in matrix f.
C=
41.0875 66.4048 31.5374
84.3670 25.9733 73.1052
68.8608 99.4798 75.5581
the vector needed like this [ 68.8608 66.4048 73.1052 ]
f=
1.0027 0.9766 1.2106
0.4001 0.5136 1.8174
1.5451 0.9488 0.8571
and because this values and positions are subject to change randomly
any help will be very thankful
  1 件のコメント
KSSV
KSSV 2019 年 2 月 8 日
REad about max

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

採用された回答

Stephen23
Stephen23 2019 年 2 月 8 日
編集済み: Stephen23 2019 年 2 月 8 日
A simple and reliable solution using linear indexing:
>> f = [1,22,333;4,33,111;1,4,33]
f =
1 22 333
4 33 111
1 4 33
>> C = [41.0875,66.4048,31.5374;84.3670,25.9733,73.1052;68.8608,99.4798,75.5581]
C =
41.087 66.405 31.537
84.367 25.973 73.105
68.861 99.480 75.558
>> [vec,idx] = max(f,[],1); % value and row index of max in each column.
>> S = size(f); % size of input array.
>> idx = idx + S(1)*(0:S(2)-1); % convert row index into linear index.
>> out = C(idx) % use linear index to get elements of C.
out =
84.367 25.973 31.537
>> vec
vec =
4 33 333
  2 件のコメント
Ashraf Sherif
Ashraf Sherif 2019 年 2 月 8 日
yeah you are completly right Mr. Stephen Cobeldick and it is work well, thank you a lot,
Ashraf Sherif
Ashraf Sherif 2019 年 2 月 12 日
How to add a row vector when my M is 4 dimensional
F=random('exp',1,3,3,4);
C=F*100
M=bsxfun(@times, C./cumsum(C,3) , reshape(1:4,1,1,[]) );
[vec,idx] = max(M,[],1); % value and row index of max in each column.
S = size(M); % size of input array.
idx = idx + S(1)*(0:S(2)-1); % convert row index into linear index !
C(idx) % use linear index to get elements of C.

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

その他の回答 (3 件)

madhan ravi
madhan ravi 2019 年 2 月 8 日
C(any(f(:)==max(f),2)).'
  13 件のコメント
Stephen23
Stephen23 2019 年 2 月 8 日
Note that this is very fragile code, and it can easily fail when values repeat in matrix f:
>> f = [1,22,333;4,33,111;1,4,33]
f =
1 22 333
4 33 111
1 4 33
>> C = [41.0875,66.4048,31.5374;84.3670,25.9733,73.1052;68.8608,99.4798,75.5581]
C =
41.087 66.405 31.537
84.367 25.973 73.105
68.861 99.480 75.558
>> C(any(f(:)==max(f),2)).'
ans =
84.367 25.973 99.480 31.537 75.558 % <- why five output values?
See my answer for code that actually delivers what the question asked for: " I want to select 3 elements from the matrix C, one element from each column..."
madhan ravi
madhan ravi 2019 年 2 月 8 日
Agree with Stephen , @Ashraf accept Stephen's answer.

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


Ashraf Sherif
Ashraf Sherif 2019 年 2 月 8 日
Yes It's work I tried it on MATLAB cloud by my phone. thank you very much my friend.

Ashraf Sherif
Ashraf Sherif 2019 年 2 月 8 日
編集済み: Ashraf Sherif 2019 年 2 月 8 日
Mr. Stephen Cobeldick and Mr. madhan ravi I appreciate you help, this was sloved my problem when I have a simple matrix 3 by 3, but now am facing an other problem that when I want to calculate new index matrix. here is my code with some explaining for what am looking for
clear all
close all
clc
for u=[2:30]% the size of matrix
x1=[];x2=[];x3=[];x4=[];x5=[];x6=[];x6=[];x7=[];x8=[];x9=[];% this is number of elements in may matrix when it was 3 by 3
for i=1:4 % number of itteration (time slot in my case)
f=random('exp',1,u,20); % size of matrix will change from 2 by 20 up to 30 by 20
Pt=1; % Transmit power 1 watt
B=50*10^3; % Bandwidth = 50 KHz * 10^3 Hz
d=5; % Distance 5 meter
lamda= -3; % Used in Urban area
No=10^-8; % AWGN noise
SNR=Pt*d^lamda*f/(B*No); % Signal-to-Noise Ratio
%% Capacity when Max-SNR and Proportional Fairness %%
C=(B/3*log2(1+SNR))/1000; % matrix of my data
% calculting the instantaneous data rate / average data rate for each user
%1- Instantaneous values of elements
x1=[x1;C(1,1)]; x2=[x2;C(1,2)];x3=[x3;C(1,3)];x4=[x4;C(2,1)];x5=[x5;C(2,2)];x6=[x6;C(2,3)];x7=[x7;C(3,1)];x8=[x8;C(3,2)];x9=[x9;C(3,3)];
% 2- Average of each elements during for example 4 itterations
M1=C(1,1)/mean(x1);M2=C(1,2)/mean(x2);M3=C(1,3)/mean(x3);M4=C(2,1)/mean(x4);M5=C(2,2)/mean(x5);M6=C(2,3)/mean(x6);M7=C(3,1)/mean(x7);M8=C(3,2)/mean(x8);M9=C(3,3)/mean(x9);
% this is my index matrix
PFindex=[M1,M2,M3;M4,M5,M6;M7,M8,M9];
maxPF=[max([M1,M4,M7]), max([M2,M5,M8]), max([M3,M6,M9])];
MaxSNR=sum(max(C))
% Capacity with Proportional Fairness
[vec,idx] = max(PFindex,[],1); % value and row index of max in each column.
S = size(PFindex); % size of input array.
idx = idx + S(1)*(0:S(2)-1); % convert row index into linear index.
PF = sum(C(idx)) % use linear index to get elements of C.
end
end
fainally I'm collecting Values of MaxSNRs and PF, thank you any way.
  2 件のコメント
Stephen23
Stephen23 2019 年 2 月 9 日
編集済み: Stephen23 2019 年 2 月 9 日
Replace all of those numbered variables, use indexing instead.
Numbered variables are a sign that you are doing something wrong.
Ashraf Sherif
Ashraf Sherif 2019 年 2 月 9 日
Thank you Stephen that what am going to do

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by