problem with tbl,xvar,yvar form of plot()

3 次查看(过去 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
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
can be more succinctly written using reshape and permute:
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)
ans = logical
1

请先登录,再进行评论。

采纳的回答

Voss
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 个评论
Maciej
Maciej 2024-6-3
I'll accept this, it answers the question just as it is written.
I have a follow up-however. What if the number of chains and the number of dimensions (aka parameters) are variables that can be configured at the start of the run (lets limit ourselves to 3 dimensions max)?
For e.g. what if i have 6 chains operating in 3 dimensional space?
I could use simple if-elseif-end to switch between plot and plot3 but not sure how to generate those (:,[numbers]) expressions programmaticaly : O
Voss
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 CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by