# How to store data whose dimension varies at each steps of run?

2 visualizzazioni (ultimi 30 giorni)
Souarv De il 8 Giu 2021
Modificato: Souarv De il 9 Giu 2021
I have a program, let say it runs for N times. So say b=1:N. Now at each iteration I get an array A whose size varries in the order of 1 by M where M is unknown (created by the nature of the pogramme). M changes with the change of iteration. Now my question is how to store this type of inodrmation in the following format or any other. For example,
b data
1 1
2 1 2 3
3 1 4 5 6
4 8
and so
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Scott MacKenzie il 8 Giu 2021
This is a bit long-winded (and can surely be shortened), but I think it achieves what you are looking for:
N = 8; % iterations
M = 5; % array size varies from 1 to 5 randomly in each iteration
% save the data in cell array C
C = {};
for b=1:N
% generate the numbers for this iteration (numbers and array size varies)
A = randi(9,1,randi([1 M]));
% convert numbers to cell array and store in C
C{b} = num2cell(A);
end
% we're done (data in C)
% output the data saved
fprintf('%2s %5s\n', 'b', 'data');
for i=1:N
fprintf('%2d ', i);
fprintf('%d ', cell2mat(C{i}));
fprintf('\n');
end
Output:
b data
1 6 7 6
2 7 8
3 1 2 3 1
4 9 4 8 3
5 2 4 6
6 4
7 6
8 2 8 3 1 7
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Souarv De il 9 Giu 2021
Thank You Scott.

Accedi per commentare.

### Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by