elimination of the chosen elements and rearranging the matrix

3 visualizzazioni (ultimi 30 giorni)
a=[1 2 3 4 5 6 7 8 nan nan nan nan nan];
for i=1:length(a)
b(i)=a(i)+1;
if b(i)< 4
b(i)=[];
elseif b(i)>6
b(i)=[];
end
end
it gives ans:
a =
1 2 3 4 5 6 7 8 NaN NaN NaN NaN NaN
>> b
b =
0 0 4 5 6 0 0 0 NaN NaN NaN NaN NaN
well, i want to eliminate the elements instead of replacing it by zeros as:
4 5 6 nan nan nan nan nan nan nan nan nan nan

Risposta accettata

Guillaume
Guillaume il 10 Lug 2019
Deleting elements of an array while you iterate over elements of an array is never going to work.
In any case, a loop is not needed for what you're doing:
a=[1 2 3 4 5 6 7 8 nan nan nan nan nan];
b = a + 1
b(b < 4 | b > 6) = [];
  1 Commento
Alchemist
Alchemist il 10 Lug 2019
what if the length of 'b' needs to be length of 'a', with remaining elements value as 'nan'

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by