Remove NaN inside a loop cycle

11 Ansichten (letzte 30 Tage)
luca
luca am 16 Sep. 2019
Beantwortet: Star Strider am 16 Sep. 2019
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?

Akzeptierte Antwort

Star Strider
Star Strider am 16 Sep. 2019
Try this:
B(i,:) = X(i,~isnan(X(i,:)));

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by