Remove NaN inside a loop cycle

1 次查看(过去 30 天)
Given the following code
V = [ 19 15 11 2 16 3 1 18 14 3 18 19 1 13 16 14 19 15 4 14 3 1 2 16 4 3 1 19 3 20 4 13 1 15 2 18 4 1 19 17 3 1 13 3 4 17 18 19 14 ;
19 16 11 2 16 15 1 18 14 3 18 19 14 2 16 14 19 15 4 14 3 11 2 16 4 3 1 19 3 20 4 13 1 4 2 18 4 1 19 17 3 19 13 3 4 17 18 19 14 ];
A= [0 15 11 2 4 0 3 1 13;
14 0 16 0 0 0 0 0 0;
0 0 0 0 8 0 0 0 0;
0 0 18 0 0 0 0 0 0;
0 0 19 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0];
%V = [V(1,:), NaN(1, mod(-size(V,2), 10))];
for i= 1:size(V,1)
VVV = [V(i,:), NaN(1, mod(-size(V,2), 10))];
%assert(mod(numel(V), 10) == 0, 'V length is not a multiple of 10');
V10 = reshape(VVV, 10, []); %rearrange V in batches of 10
[~, where] = ismember(V10, A); %find location of elements in A
[~, col] = ind2sub(size(A), where); %convert location into column index
[~, order] = sort(col, 1); %and sort by column order
X (i,:) = reshape(V10(order + (0:size(order, 1):numel(order)-1)), 1, []); %and rearrange columns of V10 according to that order
B(i,:) = X(~isnan(X(i,:)))';
end
I'm generating a matrix X where inside are contained some NaN. How can I remove the NaN inside the rows?
the code
B(i,:) = X(~isnan(X(i,:)))';
does not work!
The number of NaN is the same in each rows, so we don't need to use cells
May someone help me?

采纳的回答

Star Strider
Star Strider 2019-9-16
Try this:
B(i,:) = X(i,~isnan(X(i,:)));

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by