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

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 Kommentar

Are you going to explain to us what the pattern of NaNs is supposed to be in the output N?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 2 Apr. 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.
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

Gefragt:

am 2 Apr. 2021

Beantwortet:

am 2 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by