How can I recode certain values in one vector based on those in another?

조회 수: 3 (최근 30일)
I'd like to get some assistance with performing calculations across two different vectors and recoding certain values if a specific criteria is met. My problem is a little unique, so please bare with me while I explain.
I have two vectors.
Vector1 = [29, 59, 94, 133, 169, 207, 243, 281, 317, 356, 392, 431, 469, 505, 543]';
Vector2 = [44, 74, 120, 148, 184, 227, 269, 296, 332, 371, 407, 451, 494, 520]';
As you'll note, the values in Vector2 are sandwiched between those from Vector1. What I'd like to do is write some code that 1) checks whether any values in Vector2 exceed the midpoint between the values that occur either side of it in Vector1 and 2) if so, recode those values in Vector2 so they equal the midpoint.
For example, the difference between elements 1 (29) and 2 (59) in Vector1 is 30 (frames), therefore the midpoint between these values is 44 (29 + (30 / 2) = 44). As a result, element 1 (44) in Vector2 should be left UNCHANGED as this value is not larger than the calculated midpoint. However, the midpoint between elements 3 (94) and 4 (133) is about 114 (94 + (39 / 2) = 114), and given the corresponding value in Vector2 exceeds this, I want to recode element 3 in Vector2 from 120 to 114 so its represents the midpoint.
The aim, of course, will be to check all values in Vector2 against their counterparts from Vector1 and recode ONLY those that are larger than the midpoint.
I'm stuck on how to go about this problem, so any feedback will be greatly appreciated. Vector1 will always have one extra element given these values sandwich those from Vector2.

채택된 답변

KSSV
KSSV 2022년 6월 24일
Vector1 = [29, 59, 94, 133, 169, 207, 243, 281, 317, 356, 392, 431, 469, 505, 543]';
Vector2 = [44, 74, 120, 148, 184, 227, 269, 296, 332, 371, 407, 451, 494, 520]';
v1 = [Vector1(1:end-1) Vector1(2:end)] ; % arrange vector2
MP = mean(v1,2) ; % get midpoint
iwant = Vector2 ; % initialize the required
idx = MP~=iwant ; % get indices where not equal to mid-point
iwant(idx) = MP(idx) % repalce the mid-point values at indices

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Schedule Model Components에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by