Using index vector NAME to index from a vector within two independent for loops

조회 수: 5 (최근 30일)
Suppose I have 3 vectors as below with the same variable name LocP generated as a result of a for loop. The number in front of the variable name LocP just represents that the variable was generated on the Nth loop.
Suppose from an independent for loop I have generated an index vector (IndexP) as below.
Is it possible to use the variable name (IndexP) to access the wanted elements of the LocP variables? Which in this case would be 3rd element from LocP(1), 1st element from LocP(2) and 2nd element from LocP(3).
I ask because my main project has two independent variables within the same for loop. One variable generates the data of interest and the other generates an index vector that gives information of the specific position of the data of interest within the first variable. Therefore the index elements differ every time the loop occurs therefore manually choosing a series of numbers is not going to help.
IndexP = [3 1 2];
LocP(1) = [0.026367187500000 0.085937500000000 0.146484375000000];
LocP(2) = [0.054687500000000 0.122070312500000 0.189453125000000];
LocP(3) = [0.049804687500000 0.108398437500000 0.180664062500000];
The actual code from my real project is down below.
for i = 1 : Nchannels % Number of brain activity acqusition channels
Data_Chan(1,:) = Data(i,:); % Extracts individual channel info from the data matrix
DataN_Chan(1,:) = DataN(i,:); % Extracts individual channel info from (data matrix*-1)
[PosP, LocP] = findpeaks(Data_Chan,Fs) % Find all the positive peaks and their locations in seconds
% Both PoP and LocP are row vectors same with NegP and LocN right below
[NegP, LocN] = findpeaks(DataN_Chan,Fs); % Find all the negative peaks and their locations in seconds
[PeakPo(i,iNepochs), IndexP(i,iNepochs)] = max(PosP,[],2); % Find the max positive peak and index its location
% Use the Index values obtained from IndexP and IndexN to obtain indexed values from LocP and LocN
[PeakNe(i,iNepochs), IndexN(i,iNepochs)] = max(NegP,[],2); % Find the max negative peak and index its location
%IndexP_Row = IndexP.'; - these are my attempts to do this process
%IndexN_Row = IndexN.'; - these are my attempts to do this process
%TimePP(i,iNepochs) = LocP([IndexP_Row]) - these are my attempts to do this process
end

채택된 답변

MinChul Park
MinChul Park 2023년 10월 5일
This has been resolved - just needed to change the array structure a bit and it worked!
The main issue was due to the compatibility between different array structures.

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by