How can I add data to an existing double at specific points?
32 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ruth Ronalter
le 12 Août 2021
Commenté : Ruth Ronalter
le 19 Août 2021
Hello,
I have this 14x1 double, I need to add 3 more data points at specific locations, effectively making it a 17x1 double. It looks like this named mean_RT
765
413
309.006493506494
288.863636363636
562.833333333333
408
925.333333333333
605
201.800000000000
530
431.666666666667
640.659574468085
625.500000000000
851.666666666667
advars is only applicable on tables, I tried expanding it with undefined values as
mean_RT = nan(17,1)
but that just creates a double of 17 Nans. I tried indexing a 0 at the points I needed (these are test subject averages that need to be the same location for statistical purposes)
mean_RT([2,13,14]) = 0
and while it does insert a zero at the technical point, instead of adding a value, it replaces the value so my double is still 14x1. It I try for separate points such as
mean_RT([2,1],[13,1],[14,1])= 0
I wind up with a 14x13x14 double. What am I doing wrong?
0 commentaires
Réponse acceptée
Dave B
le 12 Août 2021
The easy to think about solution might feel a little messy:
% insert the numbers 13 and 18 between a(2)/a(3) and a(4)/a(5)
a=1:5
b=[a(1:2) 13 a(3:4) 18 a(5)]
How about taking this strategy, make the new array all NaN, assign the new values, then assign the old array to the remaining NaNs?
a = 1:5;
b = nan(1,7);
b([3 6]) = [13 18];
b(isnan(b)) = a
4 commentaires
Dave B
le 13 Août 2021
Modifié(e) : Dave B
le 13 Août 2021
WRT the first approach, I used a row vector as an example, but your data is in a column vector. That just means you need a ; between each value instead of a space. In the second approahc, I was explictly relying on the fact that the inserted data is not NaN, so when you inserted three NaN values I'd expect it to error.
Because we can now run code right in ML Answers, I'll just run my suggestions with the data you included below. To make matters just a tiny bit more confusing, I'll display the results using a row vector (because otherwise the display will just show ... after a few entries).
mean_RT=[765;413;309.006493506494;288.863636363636;562.833333333333;408;925.333333333333;605;201.800000000000;530;431.666666666667;640.659574468085;625.500000000000;851.666666666667];
First approach (simple):
new_mean_RT = [mean_RT(1); 0; mean_RT(2:11); 0; 0; mean_RT(12:end)];
disp(new_mean_RT')
Second approach (as a tip, to understand exactly how this works take off the semi-colons and run each line separately. I broke out into an extra line just so that it's clear what's happening.
new_mean_RT = nan(17,1);
new_mean_RT([2 13 14])=0;
ind=isnan(new_mean_RT);
new_mean_RT(ind)=mean_RT;
disp(new_mean_RT')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!