problem with tbl,xvar,yvar form of plot()
2 次查看(过去 30 天)
显示 更早的评论
Greetings, as per title i have a problem with tbl,xvar,yvar form of plot().
I have an array with history of the evolution of the parameters of a function during parameter optimization.
num_iterations = size(param_history, 1);
num_chains = size(param_history, 2);
num_dimensions = size(param_history, 3);
param_at_step = zeros(num_iterations, num_chains * num_dimensions);
The array is of the form 10000x8 - 10k iterations by 2 parameters for each of the 4 independent optimization chains. I expected:
for iteration = 1 : num_iterations
for chain_id = 1 : num_chains
for param = 1 : num_dimensions
column = ((chain_id-1) * 2) + param;
param_at_step(iteration, column) = param_history(iteration, chain_id, param);
end
end
end
plot( param_at_step, [1 3 5 7], [2 4 6 8] );
to plot the evolution of the parameters in the parameter space, but i get an error:
Error using plot
Specify the coordinates as vectors or matrices of the same size, or as a vector and a matrix that share the same length in at least one dimension.
I am confused, what am I doing wrong?
1 个评论
Voss
2024-6-3
The code involving three nested for loops for constructing param_at_step from param_history
param_history = randn(10000,4,2); % random data
num_iterations = size(param_history, 1);
num_chains = size(param_history, 2);
num_dimensions = size(param_history, 3);
param_at_step = zeros(num_iterations, num_chains * num_dimensions);
for iteration = 1 : num_iterations
for chain_id = 1 : num_chains
for param = 1 : num_dimensions
column = ((chain_id-1) * 2) + param;
param_at_step(iteration, column) = param_history(iteration, chain_id, param);
end
end
end
num_iterations = size(param_history, 1);
param_at_step_test = reshape(permute(param_history,[1 3 2]),num_iterations,[]);
And the result is the same:
isequal(param_at_step_test,param_at_step)
采纳的回答
Voss
2024-6-3
what am I doing wrong?
param_at_step is a numeric matrix, not a table, so you are not using the tbl,xvar,yvar form of plot().
3 个评论
Voss
2024-6-3
编辑:Voss
2024-6-3
In my opinion, it's easier to plot from the 3D array param_history rather than rearranging it into the matrix param_at_step first.
param_history = randn(10000,6,3); % random data
[num_iterations,num_chains,num_dimensions] = size(param_history);
Here's one simple way:
figure
if num_dimensions > 2
plot3(param_history(:,:,1),param_history(:,:,2),param_history(:,:,3))
else
plot(param_history(:,:,1),param_history(:,:,2))
end
Here's another way:
if num_dimensions > 2
plot_func = @plot3;
else
plot_func = @plot;
end
plot_args = permute(num2cell(param_history,1),[3 2 1]);
figure
plot_func(plot_args{:})
If you need to use param_at_step for whatever reason, then here's how you can do it, generating the vectors of column numbers programmatically:
param_at_step = reshape(permute(param_history,[1 3 2]),num_iterations,[]);
figure
if num_dimensions > 2
plot3(param_at_step(:,1:num_dimensions:end),param_at_step(:,2:num_dimensions:end),param_at_step(:,3:num_dimensions:end))
else
plot(param_at_step(:,1:num_dimensions:end),param_at_step(:,2:num_dimensions:end))
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!