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 :)
1 个评论
Matt J
2021-4-2
Are you going to explain to us what the pattern of NaNs is supposed to be in the output N?
回答(2 个)
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.
0 个评论
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),:)=[]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!