Leave one out of an array in a for loop

2 visualizzazioni (ultimi 30 giorni)
Joana
Joana il 20 Feb 2021
Risposto: Deepak il 21 Ago 2024
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

Risposte (1)

Deepak
Deepak il 21 Ago 2024
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by