The same function behaves differently, why? - matlab
I have this function below which works perfecttly as I need:
function [pointsQRS, pointsP, pointsT] = VCG (pointsQRS,pointsP,pointsT)
global ax1 ax2 h
figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8])
%% first axes
ax1=subplot(1,2,1);
set(ax1,'Position',[0.1,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([0 10])
plotCurve
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% second axes
ax2=subplot(1,2,2);
set(ax2,'Position',[0.6,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([10 10])
plotCurve
function plotCurve
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-
1:i],2),pointsQRS([i-1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-
1:i],2),pointsQRS([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-
1:i],3),'-b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
xlabel('Vx');ylabel('Vy');zlabel('Vz');
end
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% Slider Rotace
S = uicontrol('Style','slider',...
'Position',[10 10 300 20],...
'Max',180,...
'Min',-180,...
'Value',0,...
'SliderStep',[1/360 1/360]);
LS=addlistener(S,'ContinuousValueChange',#slider_callback);
set(S,'UserData',LS)
end
function slider_callback(hObject,eventData)
global ax1 ax2
val = get(hObject,'value');
view(ax1,[val,10])
view(ax2,[val+10,10])
end
But when I changed the code, simplified, because in other function I need only pointsT it gives me the error as in the picture.
The simplified code is:
function pointsT = VCG_T (pointsT)
global ax1_T ax2_T h_T
figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8])
%% first axes
ax1_T=subplot(1,2,1);
set(ax1_T,'Position',[0.1,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([0 10])
plotCurve
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% second axes
ax2_T=subplot(1,2,2);
set(ax2_T,'Position',[0.6,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([10 10])
plotCurve
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% Slider Rotace
S = uicontrol('Style','slider',...
'Position',[10 10 300 20],...
'Max',180,...
'Min',-180,...
'Value',0,...
'SliderStep',[1/360 1/360]);
LS=addlistener(S,'ContinuousValueChange',#slider_callback);
set(S,'UserData',LS)
end
function slider_callback(hObject,eventData)
global ax1_T ax2_T
val = get(hObject,'value');
view(ax1_T,[val,10])
view(ax2_T,[val+10,10])
end
The picture of the error:
I have literally no idea why the simplified code doesn't work, probably I'm overlooking some thing.
Could you please give me a hint?
New problem below:
You have removed the line function plotCurve. This is an important one since it defines a local function that the main function VCG calls. In your reduced example this function does not exist anymore since you removed its header. That's why you see this error.
Just put it back (before the first loop), then it should work.
Related
Getting the matrix value given a vector of indices along each axis
I have a Matlab matrix M with size: [70 5 3 2 10 9 5 3 21]; I have a vector with a coordinates that I want to read of that matrix: [5, 1, 1, 2, 3, 4, 1, 2, 1]; MWE example of what I am trying to get: M = rand(70 5 3 2 10 9 5 3 21); coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1]; % Output desired: M(5, 1, 1, 2, 3, 4, 1, 2, 1) %Current attempt: M(coordinates) Clearly M(coordinates) <> M(5, 1, 1, 2, 3, 4, 1, 2, 1). Is there a way of doing this?
It's a bit awkward, but you can convert the array to a cell array, and then to a comma-separated list: M = rand(70, 5, 3, 2, 10, 9, 5, 3, 21); coordinates = [5, 1, 1, 2, 3, 4, 1, 2, 1]; coords_cell = num2cell(coordinates); result = M(coords_cell{:});
Constant error in neural network, MatConvNet
Solved: Previously my dataset had around 1000 images. I increased it to 50 000 and now the neural network learns and works. I have created a convolutional neural network for recognizing three emotions from facial expression(positive, neutral, negative emotion). Somehow, my error function does not get any better(error image). Training and validation error are constant for 100 epochs. What could be the reason? Why the error is constant? Here's my code: function training(varargin) setup ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% rngNum = 1; % rng number for random weight initialization, e.g., 1,2,3 num_fcHiddenNeuron =1024; % # neurons in the fully-connected hidden layer prob_fcDropout = 0.5; % dropout probability in the fully-connected hidden layer, %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % input data for training deep CNNs imdb1 = load(['trainingdata']) ; imdb2 = load(['testdata']) ; imdb.images.data = cat(4, imdb1.images.data, imdb2.images.data); imdb.images.labels = cat(2, imdb1.images.labels, imdb2.images.labels); imdb.images.set = cat(2, imdb1.images.set, imdb2.images.set); imdb.meta = imdb1.meta; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% trainOpts.batchSize = 200 ; trainOpts.numEpochs = 100 ; trainOpts.gpus = [] ; trainOpts.continue = true ; trainOpts.learningRate = [0.004*ones(1,25), 0.002*ones(1,25), 0.001*ones(1,25), 0.0005*ones(1,25)]; trainOpts = vl_argparse(trainOpts, varargin); %% Training Deep CNNs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CNN configuration net.layers = {} ; % % % % %% Conv1 - MaxPool1 rng(rngNum) %control random number generation net.layers{end+1} = struct('type', 'conv', ... 'weights', {{0.01*randn(3,3,1,32, 'single'), 0.1*ones(1, 32, 'single')}}, ... 'stride', 1, ... 'pad', 1, ... 'filtersLearningRate', 1, ... 'biasesLearningRate', 1, ... 'filtersWeightDecay', 1/5, ... 'biasesWeightDecay', 0) ; net.layers{end+1} = struct('type', 'relu') ; net.layers{end+1} = struct('type', 'pool', ... 'method', 'max', ... 'pool', [2 2], ... 'stride', 2, ... 'pad', 0) ; % %%% Conv2 - MaxPool2 rng(rngNum) net.layers{end+1} = struct('type', 'conv', ... 'weights', {{0.01*randn(3,3,32,32, 'single'), 0.1*ones(1, 32, 'single')}}, ... 'stride', 1, ... 'pad', 0, ... 'filtersLearningRate', 1, ... 'biasesLearningRate', 1, ... 'filtersWeightDecay', 1/5, ... 'biasesWeightDecay', 0) ; net.layers{end+1} = struct('type', 'relu') ; net.layers{end+1} = struct('type', 'pool', ... 'method', 'max', ... 'pool', [2 2], ... 'stride', 2, ... 'pad', [1, 0, 1, 0]) ; % %%% Conv3 - MaxPool3 rng(rngNum) net.layers{end+1} = struct('type', 'conv', ... 'weights', {{0.01*randn(3,3,32,64, 'single'), 0.1*ones(1, 64, 'single')}}, ... 'stride', 1, ... 'pad', 1, ... 'filtersLearningRate', 1, ... 'biasesLearningRate', 1, ... 'filtersWeightDecay', 1/5, ... 'biasesWeightDecay', 0) ; net.layers{end+1} = struct('type', 'relu') ; net.layers{end+1} = struct('type', 'pool', ... 'method', 'max', ... 'pool', [2 2], ... 'stride', 2, ... 'pad', 0) ; % %%% Fc Hidden rng(rngNum) net.layers{end+1} = struct('type', 'conv', ... 'weights', {{0.001*randn(5,5,64,num_fcHiddenNeuron, 'single'), 0.01*ones(1, num_fcHiddenNeuron, 'single')}}, ... 'stride', 1, ... 'pad', 0, ... 'filtersLearningRate', 1, ... 'biasesLearningRate', 1, ... 'filtersWeightDecay', 1/5, ... 'biasesWeightDecay', 0) ; net.layers{end+1} = struct('type', 'relu') ; net.layers{end+1} = struct('type', 'dropout', ... 'rate', prob_fcDropout) ; % % %%% Fc Output rng(rngNum) net.layers{end+1} = struct('type', 'conv', ... 'weights', {{zeros(1,1,num_fcHiddenNeuron, 3, 'single'), zeros(1, 3, 'single')}}, ... 'stride', 1, ... 'pad', 0, ... 'filtersLearningRate', 1, ... 'biasesLearningRate', 1, ... 'filtersWeightDecay', 4, ... 'biasesWeightDecay', 0) ; net.layers{end+1} = struct('type', 'softmaxloss') ; % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % starting to train deep CNN [net,info] = cnn_train(net, imdb, getBatch(opts), trainOpts, 'val', find(imdb.images.set == 2)) ; net.layers(end) = [] ; function fn = getBatch(opts) % ------------------------------------------------------------------------- fn = #(x,y) getSimpleNNBatch(x,y) ; end % ------------------------------------------------------------------------- function [images, labels] = getSimpleNNBatch(imdb, batch) % ------------------------------------------------------------------------- images = imdb.images.data(:,:,:,batch) ; labels = imdb.images.labels(1,batch) ; end
Expanding each element in a (2-by-2) matrix to a (3-by-2) block
I want to expand each element in a (2-by-2) matrix to a (3-by-2) block, using Python 3 --- with professional and elegant codes. Since I don't know the python codes, I will just describe the following in maths X = # X is an 2-by-2 matrix. 1, 2 3, 4 d = (3,2) # d is the shape that each element in X should be expanded to. Y = # Y is the result 1, 1, 2, 2 1, 1, 2, 2 1, 1, 2, 2 3, 3, 4, 4 3, 3, 4, 4 3, 3, 4, 4 Not that every element in X is now an 3-by-2 block in Y. The position of the block in Y is the same as the position of the element in X. Here is the MATLAB code X = [1,2;3,4]; d = [3,2] [row, column] = size(X); a = num2cell(X); b = cell(row, column); [b{:}] = deal(ones(d)); Y = cell2mat(cellfun(#times,a,b,'UniformOutput',false)); I appreciate your help. Thanks in advance.
If you are okay with using NumPy module with Python, you can use numpy.kron - np.kron(X,np.ones((3,2),dtype=int)) Sample run - In [15]: import numpy as np In [16]: X = np.arange(4).reshape(2,2)+1 # Create input array In [17]: X Out[17]: array([[1, 2], [3, 4]]) In [18]: np.kron(X,np.ones((3,2),dtype=int)) Out[18]: array([[1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4], [3, 3, 4, 4]]) In fact, this is a direct translation of how one would achieved the desired result in MATLAB in an elegant and professional way as well, as shown below - >> X = [1,2;3 4] X = 1 2 3 4 >> kron(X,ones(3,2)) ans = 1 1 2 2 1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4 3 3 4 4
Another way to do it with ndarray.repeat: >>> X = np.arange(4).reshape(2,2)+1 >>> X.repeat(3, axis=0).repeat(2, axis=1) array([[1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4], [3, 3, 4, 4]])
Is it possible to change the inequality behaviour of interp1 when using 'previous' or 'next'
Consider as examples: interp1([0, 1], [2, 3], 0 , 'previous') interp1([0, 1], [2, 3], 0 , 'next') which produces ans = 2 ans = 2 That is, in each respective case it finds the value in [0, 1] closest to and not exceeding 0 (respectively closest to and not below 0), then returns the corresponding value of [2,3]. I would like to change the second condition to "closest to and above 0", that is, it should return the same results as: interp1([0, 1], [2, 3], 0.1 , 'previous') interp1([0, 1], [2, 3], 0.1 , 'next') which gives ans = 2 ans = 3 In this case this works as 0.1 is a value in between [0, 1].
Shading a rectangle
Consider the rectangle formed by the vertices (0, 0), (0, 10), (1, 10), and (1, 0). How would I shade it red in MATLAB? Note. For some reason, none of the mentioned shadings here works.
You can do that for example with patch: vertices = [0 0; 0 10; 1 10; 1 0]; patch(vertices(1:end,1), vertices(1:end,2), [1 .2 .2], 'edgecolor', [0 0 0]); %// [1 .2 .2] is light red for the fill; [1 1 1] is black for the edge axis([-1 2 -10 20]); %// set axis limits to properly see rectangle