Effacer les filtres
Effacer les filtres

How to include NaN elements in an array based on a condition?

2 vues (au cours des 30 derniers jours)
Annapurna Mandalika
Annapurna Mandalika le 30 Mar 2023
Can someone help me out in writing a code to insert NaN eleemnts in an array if the element is not a consecutive multiple of 2? For example if a = [2 4 6 12 14 18]; Im expecting to be turned out as a = [2 4 6 NaN NaN 12 14 NaN 18]; Thank you

Réponse acceptée

Davide Masiello
Davide Masiello le 30 Mar 2023
Modifié(e) : Davide Masiello le 30 Mar 2023
a = [2 4 6 12 14 18];
A = a(1):2:a(end);
new_a = zeros(size(A));
idx = any(a'== A,1);
new_a(idx) = a;
new_a(~idx) = nan;
new_a
new_a = 1×9
2 4 6 NaN NaN 12 14 NaN 18

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by