How to get diagonal elements of non-square matrix?
Mostra commenti meno recenti
Hi, guys!
I am working with rectangular matrixes and I want to extract the values that go from the upper left corner to lower right (diagonal). I have tried to write some codes, but they don't give me good results (it actually gets worst as the matrix gets "more rectangular"). Does anyone have an ideia?
Thank you,
Carolina Magalhães
2 Commenti
Jan
il 26 Mar 2017
What is the wanted output for a rectangular matrix? Do you mean the diagnonal from the [1,1] element to the [n,n] element, when the n is: min(size(X))? Or do you mean the "diagonal" from the element [1,1] to [m,n], when m and n are the dimensions of the matrix? In the latter case, what is the wanted result? The nearest element or a bilinear interpolation?
turningpoint
il 26 Mar 2017
Risposta accettata
Più risposte (2)
Image Analyst
il 26 Mar 2017
Try this:
m = randi(9, 5, 12) % Sample data
[rows, columns] = size(m)
for k = 1 : min([rows, columns])
output(k) = m(k,k);
end
output % Echo to command window
2 Commenti
turningpoint
il 26 Mar 2017
Image Analyst
il 27 Mar 2017
I thought you wanted to go down the exact diagonal at 45 degrees. If you go from corner to corner without going down a 45 degree diagonal, then you'll have to interpolate values. I didn't know you wanted to interpolate - I thought you wanted to extract existing values. If you want to interpolate then improfile() is the way to go.
You can create your own interpolation method:
function D = Diagonal(M, n)
% Inputs: M: Matrix
% n: Number of points, default: length(M)
% Outputs: D: Interpolated values of diagonal with n elements
% Author: Jan Simon, License: CC BY-SA 3.0
[s1, s2] = size(M);
if nargin == 1
n = max(s1, s2); % Number of wanted points, can be defined freely
end
x = linspace(1, s2, n); % n steps along rows
xN = floor(x);
xN(n) = s2 - 1; % care about last step
xS = x - xN;
y = linspace(1, s1, n); % n steps along columns
yN = floor(y);
yN(n) = s1 - 1; % care about last step
yS = y - yN;
Index = sub2ind([s1, s2], yN, xN); % Get linear index
D = (M(Index) .* (1 - xS) + M(Index + s1) .* xS) .* (1 - yS) + ...
(M(Index + 1) .* (1 - xS) + M(Index + s1 + 1) .* xS) .* yS;
end
2 Commenti
turningpoint
il 26 Mar 2017
Christopher Gordon
il 20 Feb 2021
Jan,
I'd like to try your method, but I keep ending up with a vector instead of the expanded array. My starting matrix is 30 columns (spectra) where each column is 1024 rows (pixels) long (1024 x 30 overall). Those spectra were taken at various points along my detectors 1024 pixel chip at randome points. The first is at pixel 13 and the last one is at pixel 1010, with 28 other pixels in between. What I would like to do is diagonally interpolate/extrapolate to see what the spectrum would be at each of the 1024 pixels. Is this possible with your function?
Categorie
Scopri di più su Operating on Diagonal Matrices in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!