how to store and display values coming from the for loop to an array variable.

1 view (last 30 days)
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

Answers (1)

Geoff Hayes
Geoff Hayes on 27 Apr 2019
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!