Extract elemnts from a matrix and put them in a new one

3 次查看(过去 30 天)
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 :)

回答(2 个)

Image Analyst
Image Analyst 2021-4-2
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.

Matt J
Matt J 2021-4-2
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),:)=[]
N = 3×5
3.6500 5.4500 5.4400 3.4500 3.2300 NaN 6.7800 NaN 9.0000 NaN NaN 9.5400 NaN NaN NaN

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by