how to store and display values coming from the for loop to an array variable.
이전 댓글 표시
for ...
....
[m n]=min(MSE);
MSE(n);//how this value of each ittration is store in an array?? And How can I display It?
end
답변 (1개)
Geoff Hayes
2019년 4월 27일
Aishwarya - if each iteration of your loop creates a variable (of some kind) that you want to store or save for further processing (or display) then you would initialize an array (or matrix) to store that data and update it on each iteration of your loop. For example, if you know how many iterations there are for your loop then
% 1. each iteration creates a scalar
numberOfIterations = 42;
scalarData = zeros(numberOfIterations, 1);
for k = 1:numberOfIterations
scalarData(k) = k^2 + k + 23; % or whatever, this is just an example
end
or
% 2. each iteration creates the same sized array
numberOfIterations = 42;
arrayOutputLength = 13;
matrixData = zeros(numberOfIterations, arrayOutputLength);
for k = 1:numberOfIterations
matrixData(k,:) = randi(1,arrayOutputLength);
end
or
% 3. each iteration creates the same sized matrix
numberOfIterations = 42;
matrixOutputNumberOfRows = 12;
matrixOutputNumberOfCols = 12;
matrixData = zeros(matrixOutputNumberOfRows, matrixOutputNumberOfCols, numberOfIterations);
for k = 1:numberOfIterations
matrixData(:,:, k) = rand(matrixOutputNumberOfRows,matrixOutputNumberOfCols);
end
or
% 4. each iteration creates a different sized matrix (use a cell array)
numberOfIterations = 42;
cellData = cell(numberOfIterations, 1);
for k = 1:numberOfIterations
cellArray{k} = rand(randi(42), randi(12));
end
or something similar to the above.
As for displaying your data it will depend upon what it is...but you may want to start with plot
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!