I have the following problem in my simulation.
A is an array 24 x 2. I am going to split it and get 4 or 12 array. It means that I group 6 or 2 array. It will be ok, if I use even "split" coefficient. If it is odd, I can"t split A.[ I can't group 5 or 7, because of 24/5=4*5 + 4 ( or 5*5 -1) or 24/7=7*3+3.
That's why I going to do the following:
If I have 24 x 2 and need group every 5 together:
block 1 : A(1,:), A(2,:),A(3,:),A(4,:),A(5,:)
block 2 : A(6,:), A(7,:),A(8,:),A(9,:),A(10,:)
block 3 : A(11,:), A(12,:),A(13,:),A(14,:),A(15,:)
block 4 : A(16,:), A(17,:),A(18,:),A(19,:),A(20,:)
block 5 : A(21,:), A(22,:),A(23,:),A(24,:), ?
As you can see the 5th block is not full, Matlab gives me an error. My idea is to create A(25,:)=0. For my simulation it will be ok.
I am going to simulate it as function:
A=rand(m,n)
w- # number of a vector that i would like group together ( in ex., it is `5`)
if mod(w,2)==0
if mod(m,2)==0
% do....
else
% remainder = 0
end
else
if mod(m,2)==0
% remainder = 0
else
%do...
end
I was going to simulate like above, but then I have noticed that it doesn't work. Because 24/10 = 2*10+4. So I should write something else
I can find the reminder as r = rem(24,5). As an example above, MatLab gives me r=4. then I can find a difference c= w-r =1 and after that, I don't know how to do that.
Could you suggest to me how to simulate such a calculation?
Determine the number of blocks needed, calculate the virtual amount of rows needed to fill these blocks, and add as many zero rows to A as the difference between the virtual and actual amount of rows. Since you didn't mention, what the actual output should look like (array, cell array, ...), I chose a reshaped array.
Here's the code:
m = 24;
n = 2;
w = 5;
A = rand(m, n)
% Determine number of blocks
n_blocks = ceil(m / w);
% Add zero rows to A
A(m+1:w*n_blocks, :) = 0
% Reshape A into desired format
A = reshape(A.', size(A, 1) / n_blocks * n, n_blocks).'
The output (shortened):
A =
0.9164959 0.1373036
0.5588065 0.1303052
0.4913387 0.6540321
0.5711623 0.1937039
0.7231415 0.8142444
0.9348675 0.8623844
[...]
0.8372621 0.4571067
0.5531564 0.9138423
A =
0.91650 0.13730
0.55881 0.13031
0.49134 0.65403
0.57116 0.19370
0.72314 0.81424
0.93487 0.86238
[...]
0.83726 0.45711
0.55316 0.91384
0.00000 0.00000
A =
0.91650 0.13730 0.55881 0.13031 0.49134 0.65403 0.57116 0.19370 0.72314 0.81424
0.93487 0.86238 0.61128 0.15006 0.43861 0.07667 0.94387 0.85875 0.43247 0.03105
0.48887 0.67998 0.42381 0.77707 0.93337 0.96875 0.88552 0.43617 0.06198 0.80826
0.08087 0.48928 0.46514 0.69252 0.84122 0.77548 0.90480 0.16924 0.82599 0.82780
0.49048 0.00514 0.99615 0.42366 0.83726 0.45711 0.55316 0.91384 0.00000 0.00000
Hope that helps!
I'm trying to display an array as a figure in MATLAB using coloured textbox that varies according to the value at that location.
So far, I have tried to use the MATLAB Edit Plot Tool to draw such a figure and then generate the code to see what it might look like. Here is what I came up with:
figure1=figure
annotation(figure1,'textbox',...
[0.232125037302298 0.774079320113315 0.034810205908684 0.0410764872521246],...
'String','HIT',...
'FitBoxToText','off',...
'BackgroundColor',[0.470588235294118 0.670588235294118 0.188235294117647]);
annotation(figure1,'textbox',...
[0.27658937630558 0.774079320113315 0.034810205908684 0.0410764872521246],...
'String',{'STAY'},...
'FitBoxToText','off',...
'BackgroundColor',[1 0 0]);
Here the result does not look so good. I'd like something neat and not as hard to write. Visually, I'd like something like this:
I've found a possible solution using the pcolor function.
Warning: I've tested it only with Octave
If you want to create a (m x n) table with, as per your picture, 4 colour, you have to:
create an array with size (m+1 x n+1) of integers' in the1:4` range setting them according to the desired order
call pcolor to plot the table
adjust the size of the figure
create your own colormap according to the desired colors
set the `colormap'
add the desired text using the text function
set the tick and ticklabel of the axes
Edit to answer the comment
In the following you can find a possible implementation of the proposed solution.
The code creates two figure:
In the first one wil be ploted the values of the input matrix
In the second one the user defined strings
The association "color - value" is performed through the user-defined colormap.
Since in the matrix x there are 4 different possible values (it has been defined as x=randi([1 4],n_row+1,n_col+1);) the colormap has to consists of 4 RGB entry as follows.
cm=[1 0.3 0.3 % RED
0.3 0.3 1 % BLUE
0 1 0 % GREEN
1 1 1]; % WHITE
Should you want to change the association, you just have to change the order of the rows of the colormap.
The comments in the code should clarify the above steps.
Code updated
% Define a rnadom data set
n_row=24;
n_col=10;
x=randi([1 4],n_row+1,n_col+1);
for fig_idx=1:2
% Open two FIGURE
% In the first one wil be ploted the values of the input matrix
% In the second one the user defined strings
figure('position',[ 1057 210 606 686])
% Plot the matrix
s=pcolor(x);
set(s,'edgecolor','w','linewidth',3)
% Define the colormap
%cm=[1 1 1
% 0 1 0
% 0.3 0.3 1
% 1 0.3 0.3];
cm=[1 0.3 0.3 % RED
0.3 0.3 1 % BLUE
0 1 0 % GREEN
1 1 1]; % WHITE
% Set the colormap
colormap(cm);
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 1);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'SUR');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 2);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'DBL');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 3);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'HIT');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Write the text according to the color
[r,c]=find(x(1:end-1,1:end-1) == 4);
for i=1:length(r)
if(fig_idx == 1)
ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
else
ht=text(c(i)+.1,r(i)+.5,'STK');
end
set(ht,'fontweight','bold','fontsize',10);
end
% Create and set the X labels
xt=.5:10.5;
xtl={' ';'2';'3';'4';'5';'6';'7';'8';'9';'10';'A'};
set(gca,'xtick',xt);
set(gca,'xticklabel',xtl,'xaxislocation','top','fontweight','bold');
% Create and set the X labels
yt=.5:24.5;
ytl={' ';'Soft20';'Soft19';'Soft18';'Soft17';'Soft16';'Soft15';'Soft14';'Soft13'; ...
'20';'19';'18';'17';'16';'15';'14';'13';'12';'11';'10';'9';'8';'7';'6';'5'};
set(gca,'ytick',yt);
set(gca,'yticklabel',ytl,'fontweight','bold');
title('Dealer''s Card')
end
Table with the values in the input matrix
Table with the user-defined strings
This is an answer inspired by il_raffa's answer, but with also quite a few differences. There is no better or worse, it's just a matter of preferences.
Main differences are:
it uses imagesc instead of pcolor
it uses a second overlaid axes for fine control of the grid color/thickness/transparency etc...
The association between value - label - color is set right at the beginning in one single table. All the code will then respect this
table.
It goes like this:
%% Random data
n_row = 24;
n_col = 10;
vals = randi([1 4], n_row, n_col);
%% Define labels and associated colors
% this is your different labels and the color associated. There will be
% associated to the values 1,2,3, etc ... in the order they appear in this
% table:
Categories = {
'SUR' , [1 0 0] % red <= Label and color associated to value 1
'DBL' , [0 0 1] % blue <= Label and color associated to value 2
'HIT' , [0 1 0] % green <= Label and color associated to value 3
'STK' , [1 1 1] % white <= you know what this is by now ;-)
} ;
% a few more settings
BgColor = 'w' ; % Background color for various elements
strTitle = 'Dealer''s Card' ;
%% Parse settings
% get labels according to the "Categories" defined above
labels = Categories(:,1) ;
% build the colormap according to the "Categories" defined above
cmap = cell2mat( Categories(:,2) ) ;
%% Display
hfig = figure('Name',strTitle,'Color',BgColor,...
'Toolbar','none','Menubar','none','NumberTitle','off') ;
ax1 = axes ;
imagesc(vals) % Display each cell with an associated color
colormap(cmap); % Set the colormap
grid(ax1,'off') % Make sure there is no grid
% Build and place the texts objects
textStrings = labels(vals) ;
[xl,yl] = meshgrid(1:n_col,1:n_row);
hStrings = text( xl(:), yl(:), textStrings(:), 'HorizontalAlignment','center');
%% Modify text color if needed
% (White text for the darker box colors)
textColors = repmat(vals(:) <= 2 ,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));
%% Set the axis labels
xlabels = [ cellstr(num2str((2:10).')) ; {'A'} ] ;
ylabels = [ cellstr(num2str((5:20).')) ; cellstr(reshape(sprintf('soft %2d',[13:20]),7,[]).') ] ;
set(ax1,'XTick', 1:numel(xlabels), ...
'XTickLabel', xlabels, ...
'YTick', 1:numel(ylabels), ...
'YTickLabel', ylabels, ...
'TickLength', [0 0], ...
'fontweight', 'bold' ,...
'xaxislocation','top') ;
title(strTitle)
%% Prettify
ax2 = axes ; % create new axe and retrieve handle
% superpose the new axe on top, at the same position
set(ax2,'Position', get(ax1,'Position') );
% make it transparent (no color)
set(ax2,'Color','none')
% set the X and Y grid ticks and properties
set(ax2,'XLim',ax1.XLim , 'XTick',[0 ax1.XTick+0.5],'XTickLabel','' ,...
'YLim',ax1.YLim , 'YTick',[0 ax1.YTick+0.5],'YTickLabel','' ,...
'GridColor',BgColor,'GridAlpha',1,'Linewidth',2,...
'XColor',BgColor,'YColor',BgColor) ;
% Make sure the overlaid axes follow the underlying one
resizeAxe2 = #(s,e) set(ax2,'Position', get(ax1,'Position') );
hfig.SizeChangedFcn = resizeAxe2 ;
It produces the following figure:
Of course, you can replace the colors with your favorite colors.
I would encourage you to play with the grid settings of the ax2 for different effects, and you can also play with the properties of the text objects (make them bold, other color etc ...). Have fun !
I have an array with a set of chronological serial numbers and another source array with random serial numbers associated with a numeric value. The code creates a new cell array in MATLAB with the perfectly chronological serial numbers in one column and in the next column it inserts the associated numeric value if the serial numbers match in both original source arrays. If they don't the code simply copies the previous associated value until there is a new match.
j = 1;
A = {random{1:end,1}};
B = cell2mat(A);
value = random{1,2};
data = cell(length(serial), 1);
data(:,1) = serial(:,1);
h = waitbar(0,'Please Wait...');
steps = length(serial);
for k = 1:length(serial)
[row1, col1, vec1] = find(B == serial{k,1});
tf1 = isempty(vec1);
if (tf1 == 0)
prices = random{col1,2};
data(j,2) = num2cell(value);
j = j + 1;
else
data(j,2) = num2cell(value);
j = j + 1;
end
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
close(h);
Right now, the run-time for the code is approximately 4 hours. I would like to make this code run faster. Please suggest any methods to do so.
UPDATE
source input (serial)
1
2
3
4
5
6
7
source input (random)
1 100
2 105
4 106
7 107
desired output (data)
SR No Value
1 100
2 105
3 105
4 106
5 106
6 106
7 107
Firstly, run the MATLAB profiler (see 'doc profile') and see where the bulk of the execution time is occuring.
Secondly, don't update the waitbar on every iteration> Particularly if serial contains a large (> 100) number of elements.
Do something like:
if (mod(k, 100)==0) % update on every 100th iteration
waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
Some points:
Firstly it would help a lot if you gave us some sample input and output data.
Why do you initialize data as one column and then fill it's second in the loop? Rather initialize it as 2 columns upfront: data = cell(length(serial), 2);
Is j ever different from k, they look identical to me and you could just drop both the j = j + 1 lines.
tf1 = isempty(vec1); if (tf1 == 0)... is the same as the single line: if (!isempty(vec1)) or even better if(isempty(vec1)) and then swap the code from your else and your if.
But I think you can probably find a fast vecotrized solution if you provide some (short) sample input and output data.
I have a struct mpc with the following structure:
num type col3 col4 ...
mpc.bus = 1 2 ... ...
2 2 ... ...
3 1 ... ...
4 3 ... ...
5 1 ... ...
10 2 ... ...
99 1 ... ...
to from col3 col4 ...
mpc.branch = 1 2 ... ...
1 3 ... ...
2 4 ... ...
10 5 ... ...
10 99 ... ...
What I need to do is:
1: Re-order the rows of mpc.bus, such that all rows of type 1 are first, followed by 2 and at last, 3. There is only one element of type 3, and no other types (4 / 5 etc.).
2: Make the numbering (column 1 of mpc.bus, consecutive, starting at 1.
3: Change the numbers in the to-from columns of mpc.branch, to correspond to the new numbering in mpc.bus.
4: After running simulations, reverse the steps above to turn up with the same order and numbering as above.
It is easy to update mpc.bus using find.
type_1 = find(mpc.bus(:,2) == 1);
type_2 = find(mpc.bus(:,2) == 2);
type_3 = find(mpc.bus(:,2) == 3);
mpc.bus(:,:) = mpc.bus([type1; type2; type3],:);
mpc.bus(:,1) = 1:nb % Where nb is the number of rows of mpc.bus
The numbers in the to/from columns in mpc.branch corresponds to the numbers in column 1 in mpc.bus.
It's OK to update the numbers on the to, from columns of mpc.branch as well.
However, I'm not able to find a non-messy way of retracing my steps. Can I update the numbering using some simple commands?
For the record: I have deliberately not included my code for re-numbering mpc.branch, since I'm sure someone has a smarter, simpler solution (that will make it easier to redo when the simulations are finished).
Edit: It might be easier to create normal arrays (to avoid woriking with structs):
bus = mpc.bus;
branch = mpc.branch;
Edit #2: The order of things:
Re-order and re-number.
Columns (3:end) of bus and branch are changed. (Not part of this question)
Restore original order and indices.
Thanks!
I'm proposing this solution. It generates a n x 2 matrix, where n corresponds to the number of rows in mpc.bus and a temporary copy of mpc.branch:
function [mpc_1, mpc_2, mpc_3] = minimal_example
mpc.bus = [ 1 2;...
2 2;...
3 1;...
4 3;...
5 1;...
10 2;...
99 1];
mpc.branch = [ 1 2;...
1 3;...
2 4;...
10 5;...
10 99];
mpc.bus = sortrows(mpc.bus,2);
mpc_1 = mpc;
mpc_tmp = mpc.branch;
for I=1:size(mpc.bus,1)
PAIRS(I,1) = I;
PAIRS(I,2) = mpc.bus(I,1);
mpc.branch(mpc_tmp(:,1:2)==mpc.bus(I,1)) = I;
mpc.bus(I,1) = I;
end
mpc_2 = mpc;
% (a) the following mpc_tmp is only needed if you want to truly reverse the operation
mpc_tmp = mpc.branch;
%
% do some stuff
%
for I=1:size(mpc.bus,1)
% (b) you can decide not to use the following line, then comment the line below (a)
mpc.branch(mpc_tmp(:,1:2)==mpc.bus(I,1)) = PAIRS(I,2);
mpc.bus(I,1) = PAIRS(I,2);
end
% uncomment the following line, if you commented (a) and (b) above:
% mpc.branch = mpc_tmp;
mpc.bus = sortrows(mpc.bus,1);
mpc_3 = mpc;
The minimal example above can be executed as is. The three outputs (mpc_1, mpc_2 & mpc_3) are just in place to demonstrate the workings of the code but are otherwise not necessary.
1.) mpc.bus is ordered using sortrows, simplifying the approach and not using find three times. It targets the second column of mpc.bus and sorts the remaining matrix accordingly.
2.) The original contents of mpc.branch are stored.
3.) A loop is used to replace the entries in the first column of mpc.bus with ascending numbers while at the same time replacing them correspondingly in mpc.branch. Here, the reference to mpc_tmp is necessary so ensure a correct replacement of the elements.
4.) Afterwards, mpc.branch can be reverted analogously to (3.) - here, one might argue, that if the original mpc.branch was stored earlier on, one could just copy the matrix. Also, the original values of mpc.bus are re-assigned.
5.) Now, sortrows is applied to mpc.bus again, this time with the first column as reference to restore the original format.