How can I add data to an existing double at specific points?

40 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Dave B
Dave B il 12 Ago 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
a = 1×5
1 2 3 4 5
b=[a(1:2) 13 a(3:4) 18 a(5)]
b = 1×7
1 2 13 3 4 18 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
b = 1×7
1 2 13 3 4 18 5
  4 Commenti
Dave B
Dave B il 13 Ago 2021
Modificato: Dave B il 13 Ago 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')
765.0000 0 413.0000 309.0065 288.8636 562.8333 408.0000 925.3333 605.0000 201.8000 530.0000 431.6667 0 0 640.6596 625.5000 851.6667
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')
765.0000 0 413.0000 309.0065 288.8636 562.8333 408.0000 925.3333 605.0000 201.8000 530.0000 431.6667 0 0 640.6596 625.5000 851.6667
Ruth Ronalter
Ruth Ronalter il 19 Ago 2021
Sorry, realized I never accepted your answer. I got both methods to work, thank you for taking the time to explain it to me and making it work.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by