Clear Filters
Clear Filters

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

3 views (last 30 days)
Maciej
Maciej on 3 Jun 2024
Edited: Voss on 3 Jun 2024
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 Comment
Voss
Voss on 3 Jun 2024
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

Sign in to comment.

Accepted Answer

Voss
Voss on 3 Jun 2024
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 Comments
Maciej
Maciej on 3 Jun 2024
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 on 3 Jun 2024
Edited: Voss on 3 Jun 2024
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

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!