Leave one out of an array in a for loop

2 ビュー (過去 30 日間)
Joana
Joana 2021 年 2 月 20 日
回答済み: Deepak 2024 年 8 月 21 日
Dear all
I have a program where i need to save one variable separately and concatenate i-1 elements in another variable (Then i need to perform some data analysis these two new variables). here is my code where i am concatenating all the elements.
Can anyone please help how i can edit it according to my problem.?
for i=1:30
resultFileName = sprintf('Sub%i.mat',i); % generate result filename
load(resultFileName)
Es{i} = feat'; % Make a cell array
feat_Concatenate = vertcat(Es{:}); % concatenate the array
end

回答 (1 件)

Deepak
Deepak 2024 年 8 月 21 日
Hi @Joana, from my understanding, you have generated a “feat_Concatenate” array in which you have concatenated all the features. You want to concatenate all the rest elements (1:i-1), other than the ith element together to perform analysis on them.
To perform this task, we can use array indexing in MATLAB to save current feature and other features in different variables.
currentFeature = Es{i};
otherFeatures = Es([1:i-1, i+1:end]); % Get all elements except the i-th one
If you want to concatenate the features from 1 to i-1 and not the later ones, we can use the below notation:
otherFeatures = Es([1:i-1]);
Below is the complete MATLAB code with changes:
Es = cell(1, 30);
feat_Concatenate = cell(1, 30);
for i = 1:30
resultFileName = sprintf('Sub%i.mat', i);
load(resultFileName)
Es{i} = feat';
% Save the current feature separately
currentFeature = Es{i};
otherFeatures = Es([1:i-1, i+1:end]); % Get all elements except the i-th one
feat_Concatenate{i} = vertcat(otherFeatures{:}); % Concatenate the remaining features
% Perform your data analysis with currentFeature and feat_Concatenate{i}
end
Attaching the documentation of Array Indexing in MATLAB for reference:
I hope this helps.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by