How can I add data to an existing double at specific points?
조회 수: 63 (최근 30일)
이전 댓글 표시
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
채택된 답변
Dave B
2021년 8월 12일
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
Dave B
2021년 8월 13일
편집: Dave B
2021년 8월 13일
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')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!