dividing a diagonal matrix into same and different diagonal elements

I have a diagonal matrix as:
V = [ 0.0001 0 0 0 0;
0 0.0001 0 0 0;
0 0 3.6388 0 0;
0 0 0 5.3610 0;
0 0 0 0 6.0004];
I want to find the number of diagonal elements which are same in values. 2ndly I want to separate them in a variable, how will I do it? Here V is a 5x5 matrix, but it may be any square size matrix in general.

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2021 年 5 月 16 日
編集済み: Dyuman Joshi 2021 年 5 月 16 日
%This will give the diagonal elements in a separate variable.
z = diag(V);
%Finding the number of repeated values
y = unique(z);
n = histc(z,y);
x = n(n>1); %If there are more than 1 repeating values, answer will be an array
a = y(n>1); %Get the variables that are repeated

3 件のコメント

Dyuman Joshi
Dyuman Joshi 2021 年 5 月 16 日
z = diag(V);
y = unique(z);
n = histc(z,y);
x = n(n>1);
a = repelem(y(n>1), n(n>1)); %repeated values
fprintf('The same values are: %d.\n', n(n>1)) %message
Dyuman Joshi
Dyuman Joshi 2021 年 5 月 17 日
編集済み: Dyuman Joshi 2021 年 5 月 17 日
I tested the function as stand-alone by giving it V1 as input and it works fine. This probably what you also said. Not sure why it doesn't run in your code. I am evaluating your code and will update here.
Dyuman Joshi
Dyuman Joshi 2021 年 5 月 17 日
Is it necessary to use functions? Your code is not the optimised way to write functions.

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

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

質問済み:

2021 年 5 月 16 日

コメント済み:

2021 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by