Compare two strings from unequal length cell array and assign value

조회 수: 1 (최근 30일)
BuMatlab
BuMatlab 2023년 7월 12일
답변: Voss 2023년 7월 12일
I have a cell array named shortStranglePositions of which I am creating a unique list of strings based on the 6th column of shortStranglePositions, named callSymList. The reason why I am creating a unique list of strings (callSymList) is I have to get market data for each row in shortStranglePositions, and that takes time, and there is no need to ask for the data multiple times.
Where I am stuck is this:
I would like to logically assign the values in the 2nd column of callSymList to the 8th column in shortStranglePositions, based on a string match in the 6th column of shortStranglePositions to the 1st column of callSymList. For example, shortStranglePositions{1,8} should equal 0.6000.
I have tried multiple string compare approaches, but since the cell lengths are unequal, I am struggling to find a solution. Is there a way to do this without looping through each row of shortStranglePositions? If so, how? If not, what is the most efficient way to complete this task?
I should also mention that it will be very common for there to be multiple duplicate strings in the 6th row of shortStranglePositions.
I have included both cell arrays in the attached EXAMPLE.mat

채택된 답변

Voss
Voss 2023년 7월 12일
load('EXAMPLE.mat')
[~,idx] = ismember(shortStranglePositions(:,6),callSymList(:,1));
shortStranglePositions(:,8) = callSymList(idx,2)
shortStranglePositions = 4×8 cell array
{'08:08:20'} {[4500]} {[4465]} {[3.1000]} {[5.5500]} {'SPXW 230712C045…'} {'SPXW 230712P044…'} {[0.6000]} {'08:33:54'} {[4495]} {[4465]} {[3.4500]} {[6.0500]} {'SPXW 230712C044…'} {'SPXW 230712P044…'} {[0.6000]} {'09:15:53'} {[4485]} {[4450]} {[3.0500]} {[3.0500]} {'SPXW 230712C044…'} {'SPXW 230712P044…'} {[1.4000]} {'09:25:02'} {[4485]} {[4450]} {[ 3]} {[3.0500]} {'SPXW 230712C044…'} {'SPXW 230712P044…'} {[1.4000]}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by