# generate average numbers between two numbers inside a vector

조회 수: 6 (최근 30일)
Alberto Acri 2024년 2월 24일
편집: Star Strider 2024년 2월 24일
Hi! It is easier with the figure than with words. I want to create average numbers between two numbers and insert them instead of '0's.
V = [3.4789; 0; 0; 3.3535; 0; 0; 3.2398; 3.2263; 0; 0; 0; 3.0846; 0; 0; 2.9815; 0; 2.9430; 0; 2.9055];
r0 = [2;3;5;6;9;10;11;13;14;16;18]; %rows with 0
r1 = [1;4;7;8;12;15;17;19];
% at the moment: mean
vect_mean = {};
for b = 1:height(r1)
v1 = V(r1(b),4);
v2 = V(r1(b+1),4);
vv = [v1; v2];
vv_mean = mean(vv);
vect_mean = [vect_mean;{vv_mean}];
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Star Strider 2024년 2월 24일
편집: Star Strider 2024년 2월 24일
One approach —
V = [3.4789; 0; 0; 3.3535; 0; 0; 3.2398; 3.2263; 0; 0; 0; 3.0846; 0; 0; 2.9815; 0; 2.9430; 0; 2.9055];
r0 = [2;3;5;6;9;10;11;13;14;16;18]; %rows with 0
r1 = [1;4;7;8;12;15;17;19];
L = numel(r1);
mvc = accumarray(r1, (1:numel(r1)).', [], @(x){mean(V([r1(x) r1(min([x+1,L]))]))});
Lv = cellfun(@(x)isempty(x), mvc, 'Unif',0);
mvc([Lv{:}]) = {NaN};
mv = cell2mat(mvc);
mv = fillmissing(mv,'previous');
V([Lv{:}]) = mv([Lv{:}])
V = 19×1
3.4789 3.4162 3.4162 3.3535 3.2966 3.2966 3.2398 3.2263 3.1555 3.1555
Vbfr = buffer(V,10) % Display Entire Vector In Columns
Vbfr = 10×2
3.4789 3.1555 3.4162 3.0846 3.4162 3.0331 3.3535 3.0331 3.2966 2.9815 3.2966 2.9623 3.2398 2.9430 3.2263 2.9242 3.1555 2.9055 3.1555 0
Vm = V;
V = [3.4789; 0; 0; 3.3535; 0; 0; 3.2398; 3.2263; 0; 0; 0; 3.0846; 0; 0; 2.9815; 0; 2.9430; 0; 2.9055];
Comparison = table(V, Vm, 'VariableNames',{'Original','Filled'})
Comparison = 19×2 table
Original Filled ________ ______ 3.4789 3.4789 0 3.4162 0 3.4162 3.3535 3.3535 0 3.2966 0 3.2966 3.2398 3.2398 3.2263 3.2263 0 3.1555 0 3.1555 0 3.1555 3.0846 3.0846 0 3.0331 0 3.0331 2.9815 2.9815 0 2.9623
First_10_Rows = Comparison(1:10,:)
First_10_Rows = 10×2 table
Original Filled ________ ______ 3.4789 3.4789 0 3.4162 0 3.4162 3.3535 3.3535 0 3.2966 0 3.2966 3.2398 3.2398 3.2263 3.2263 0 3.1555 0 3.1555
Last_9_Rows = Comparison(11:end,:)
Last_9_Rows = 9×2 table
Original Filled ________ ______ 0 3.1555 3.0846 3.0846 0 3.0331 0 3.0331 2.9815 2.9815 0 2.9623 2.943 2.943 0 2.9242 2.9055 2.9055
The accumarray call calculates the mean values, then computes logical vector ‘Lv’ to detect the empty cells that are then filled with NaN values. The ‘mvc’ cell array is then converted to a numerical array, and the NaN values are filled with the previous result using the fillmissing funciton. The ‘Lv’ vector us used again to fill the zeros in the original ‘V’ vector with the corresponding elements from ‘mv. to provide the result.
EDIT — (24 Feb 2024 at 15:46)
Added ‘Comparison’ table and sub-tables from it, extended the discussion of how the code works.
.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Preprocessing Data에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by