How to write my for loop data into an array

조회 수: 10 (최근 30일)
Michael Scott
Michael Scott 2022년 6월 2일
답변: Stephen23 2022년 6월 2일
Hello I have a for loop that is extracting data from a matrix and finding the minimums and maximums. I need it to write all the maxes in a array rathter than provide individual ans for each loop.
clear all
opts = detectImportOptions('testforscripts.csv');
filename = "testforscriptsfull.csv"; %reads file of all data points
T = readtable(filename,opts); %synthesizes the data and creates a table of variables
Two0 = standardizeMissing(T, 0); %takes all values of 0 and replaces them with NaN
%create an equally spaced vector of values using the more general form start:step:end.
ElmID1 = [99109176:3:99110484]; %this is the elm ID vector
ElmID2 = [99110592:3:99110837];
row = 308; % rows per group
grp = size(Two0,1)/row; %creates smaller matrixes of each data set
for k = 1:grp
idr = (1:row)+(k-1)*row; %breaks large data into 308 data sets WITH NaN
Finalmatrix=rmmissing(Two0(idr,:)); %Removes all values of NaN
Force=(Finalmatrix(:,2)); %Extract the second coloum (force) for each loop
Array=table2array(Force); %turn the table into an array so it can be indexed
Maxes = max(Array); %maxes of each coloumn
Mins = min(Array); %mins of each coloumn
for i=1:length(Maxes)
MaxMatrix(:,i)= Maxes(:,1) *This is the part I'm stuck on, method is not working*
MaxMatrix(:,i)=[MaxMatrix , Maxes(:,1) ] *This is the part I'm stuck on, method is not working*
end
end

답변 (1개)

Stephen23
Stephen23 2022년 6월 2일
Maxes = cell(1,grp);
for k = 1:grp
..
Maxes{k} = max(Array);
end
Maxes = vertcat(Maxes{:}); % optional

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by