Arrays and different dimensions while saving outputs

조회 수: 4 (최근 30일)
Hello to everyone,
Say we have two for loops in the following form:
for i = [1, 2..., N_i]
for j = [1, 2..., N_j]
[Some code that does stuff];
output = some_vector; % This vector is of size m*1
Array{i}(:, j) = output;
end
end
So at the end, we will arrive with:
  1. A cell array named Array with N_i elements.
  2. Each element of the Array will display a matrix of m rows and N_j columns.
Now, this works fine if and only if the output has always the same size, i.e., m*1. We can't save matrices of different row lengths. So my question is the following: is there any way to store the outputs when they are of different size (e.g., the first loop gives an output of 10*1, and the second one of 15*1, and so on...)?
Thanks in advance to everyone.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 4월 6일
편집: Dyuman Joshi 2023년 4월 6일
"is there any way to store the outputs when they are of different size (e.g., the first loop gives an output of 10*1, and the second one of 15*1, and so on...)?"
Yes, Cell arrays -
m=5;
%pre-allocation
y=cell(m,1);
for p=1:m
%Storing array of size dependent on the loop index
y{p,1}=p*ones(p*m,1);
end
y
y = 5×1 cell array
{ 5×1 double} {10×1 double} {15×1 double} {20×1 double} {25×1 double}
y{3}
ans = 15×1
3 3 3 3 3 3 3 3 3 3
  댓글 수: 1
Julio Maximiliano Ramirez Oyanarte
Lol, this is embarassing, didn't know about the possibility of using {i, j} in arrays. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by