Arrays and different dimensions while saving outputs
4 次查看(过去 30 天)
显示 更早的评论
Julio Maximiliano Ramirez Oyanarte
2023-4-6
评论: Julio Maximiliano Ramirez Oyanarte
2023-4-6
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:
- A cell array named Array with N_i elements.
- 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.
0 个评论
采纳的回答
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{3}
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!