Extract elemnts from a matrix and put them in a new one
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone,
I'm looking for a command or an algorithm able to solve the following problem:
I have a matrix M in which there are many zeros with decimal values and few elements which are higher than 0.99 such as this matrix:
M=[ 0.45 5.45 0.64 0.78 0.32
3.65 0.79 0.56 0.23 0.11
0.21 6.78 0.55 3.45 0.57
0.67 9.54 5.44 9.00 3.23]
I would like to make a new matrix N by extracting the elements of matrix M which are larger than 1 with a certain arrangement like this:
N=[ 3.65 5.45 5.44 3.45 3.23
NaN 6.78 NaN 9.00 NaN
NaN 9.54 NaN NaN NaN]
Thanks for your attention :)
1 commentaire
Matt J
le 2 Avr 2021
Are you going to explain to us what the pattern of NaNs is supposed to be in the output N?
Réponses (2)
Image Analyst
le 2 Avr 2021
Try this:
M=[ 0.45 5.45 0.64 0.78 0.32
3.65 0.79 0.56 0.23 0.11
0.21 6.78 0.55 3.45 0.57
0.67 9.54 5.44 9.00 3.23]
[rows, columns] = size(M)
MOut = nan(size(M));
for col = 1 : columns
thisCol = M(:, col)
moreThan1 = thisCol(thisCol >= 1)
for row = 1 : length(moreThan1)
MOut(row, col) = moreThan1(row);
end
end
% Get rid of any all non rows
rowsToDelete = all(isnan(MOut), 2);
MOut(rowsToDelete, :) = []
You get
MOut =
3.65 5.45 5.44 3.45 3.23
NaN 6.78 NaN 9 NaN
NaN 9.54 NaN NaN NaN
If it's not your homework assignment, you can use this code.
0 commentaires
Matt J
le 2 Avr 2021
Using the attached file sortlidx.m
M=[ 0.45 5.45 0.64 0.78 0.32
3.65 0.79 0.56 0.23 0.11
0.21 6.78 0.55 3.45 0.57
0.67 9.54 5.44 9.00 3.23];
N=M;
N(N<1)=nan;
[~,is]=sortlidx(isnan(N));
N=N(is);
N(all(isnan(N),2),:)=[]
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!