Suggestions to improve script performance?
Mostra commenti meno recenti
This is the result of my script, graphically :

It's not a DaVinci but it's 'working'. I tried to use what I thought was Matlab "good" techniques but I'm sure I'm light years behind what good programmers would do... I suspect there are lot's of opportunities for improvements in my code and I would be grateful if you could point out the way. Just a few tips, I'm sure, will go a long way towards getting the improvements I'm looking for. It's not the end of the world, as it is, but you'll see, when you try out the code, that each 'presses' on the Slider takes a long time to execute :
clc
global Axe_a_b;
fig=figure('Name','Roger Breton sRGB CIE ab diagram');
Test = fig.Position;
fig.Position = [1600 500 700 700];
ax = axes(fig);
axis([-128 128 -128 128]);
xticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
yticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
hold on;
CIE_a = 128;
CIE_b = 65; % 128 / 65, Steps of 4
% Algorithm :
% Each call to the function calculates a new colormap
% using a lower b* value
% Keeping L* range constant, between each function call :
% Slice L* = 50
Axe_L = transpose(linspace(50, 50, 65));
% Create one Column vector of 65 constant a* values,
% to fill the diagram, columns by columns,
% from Left (a* = -128) to right (a* = 128)
Axe_a_b = transpose(linspace(-128, 128, 65));
% MarkerSize = 100
for row = 1:65
Col = linspace(CIE_a, CIE_a, 65)';
RowX=[Axe_a_b, Col];
colorListAB = CalculateColorList(CIE_a, CIE_b, Axe_L)
scatter(RowX(:,1),RowX(:,2),100, colorListAB, 'filled', 's');
CIE_a = CIE_a - 4;
end
uicontrol('Style', 'Slider', 'Position',[655 500 20 150], 'Value', 0.5, 'Callback', {@sliderCB});
global txt_ctrl;
txt_ctrl = uicontrol('Style', 'text', 'Position',[640 450 50 50], 'String', ['50']);
set(txt_ctrl, 'FontSize', 20);
function [colorListAB] = CalculateColorList(CIE_a,CIE_b, Axe_L)
% CIE_a = Row
% CIE_b = Col
global Axe_a_b;
% Create one Column vector of 65 constant a* values
% 1st call = a* 128
% 2nd call = a* 124
% 3rd call = a* 120 ...
Axe_L_ROW = transpose(linspace(CIE_a, CIE_a, CIE_b));
Lab_Axe_AB = [Axe_L, Axe_a_b, Axe_L_ROW];
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
[row,col] = size(RGB_Axe_AB);
% Process Green/Magenta axis first
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
colormapAB = RGB_Axe_AB_ColorMap;
colorListAB = colormapAB;
end
function sliderCB(SliderH, EventData)
global txt_ctrl;
global Axe_a_b;
CIE_a = 128;
CIE_b = 65;
% Retrieve slider current value of
Slider_L_New = SliderH.Value * 100;
set(txt_ctrl,'String',num2str(round(Slider_L_New)));
Axe_L_New = transpose(linspace(Slider_L_New, Slider_L_New, 65));
for row = 1:65
Col = linspace(CIE_a, CIE_a, 65)';
RowX=[Axe_a_b, Col];
colorListAB = CalculateColorList(CIE_a, CIE_b, Axe_L_New)
scatter(RowX(:,1),RowX(:,2),100, colorListAB, 'filled', 's');
CIE_a = CIE_a - 4;
end
end
Any help is appreciated.
7 Commenti
Cris LaPierre
il 24 Gen 2022
What are the improvements you are looking for? The more specific you are, the easier it is to provide help.
Roger Breton
il 25 Gen 2022
@Roger Breton: Can you verify that your usage of linspace() is correct everywhere? For instance, the expressions
Col = linspace(CIE_a, CIE_a, 65)';
Axe_L_ROW = transpose(linspace(CIE_a, CIE_a, CIE_b));
Axe_L_New = transpose(linspace(Slider_L_New, Slider_L_New, 65));
all have the first and second arguments to linspace() the same, which will produce a vector of repeated scalars, e.g.:
linspace(45,45,10)
Is this what you intend?
(This is not directly related to speed, but it is useful to understand what the code is supposed to be doing.)
Roger Breton
il 26 Gen 2022
Voss
il 26 Gen 2022
@Roger Breton: You can use ones() and multiply by the value you want (or use zeros() and add the value you want), or use repelem() or repmat(), e.g.:
Col = CIE_a*ones(65,1);
Axe_L_ROW = repelem(CIE_a,CIE_b,1);
Axe_L_New = repmat(Slider_L_New,65,1);
To generate a vector of constant values you can use repmat, ones, or zeros.
v1 = repmat(42, 1, 5)
v2 = 42 + zeros(1, 5)
v3 = 42 * ones(1, 5)
You can also look at the functions listed in the See Also section on the documentation pages for those functions for other related functions.
Roger Breton
il 28 Gen 2022
Risposta accettata
Più risposte (1)
Benjamin Thompson
il 25 Gen 2022
0 voti
The MATLAB editor has some helpful suggestions, removing unnecessary output and so on. Have you tried using the Profile button in the editor to run it and find the places where the most time is spent?
4 Commenti
Benjamin Thompson
il 25 Gen 2022
It seems to me you might rather construct your output in a matrix and display it as an image once, using imshow, rather than then 65 separate scatter plot calls. You are plotting all these filled squares which are basically the same as pixels right?
Roger Breton
il 26 Gen 2022
Stephen23
il 26 Gen 2022
Images are just arrays. Take a look here: https://www.mathworks.com/help/matlab/creating_plots/image-types.html#f2-12468
Updating a IMAGE graphics object would be much more efficient than what you are doing now:
Roger Breton
il 26 Gen 2022
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



