https://jp.mathworks.com/matlabcentral/answers/questionsMATLAB Answers — 新規の質問2020-03-29T01:29:17Ztag:jp.mathworks.com,2005:Question/5134992020-03-28T03:24:43Z2020-03-29T01:29:17Zhow to split a stringi need to create a function named breakline which inputs a string
(char, 1 × N) and a linewidth (integer, scalar). The function then splits
the string into smaller strings which are smaller than the linewidth.
The output to the function is an array which contains all of the split up
lines (cell array, 1 × M).
for example
out=breakupLine(’123456789’,4)
out =
{
[1,1] = ’1234’
[1,2] = ’5678’
[1,3] = ’9’
}Mitchie Teoticohttps://jp.mathworks.com/matlabcentral/profile/authors/17206687-mitchie-teoticotag:jp.mathworks.com,2005:Question/4193952018-09-17T15:41:38Z2020-03-29T01:28:29ZRemove empty rows from matrixHi folks,
I have the following matrix:
0.6857 0.1248 0.9636 1.0884 0.4807 0.6077 0.3644 0 0 0
0.9093 1.0006 1.0006 1.0006 1.0006 0.3702 0 0 0 0
0.6367 1.0106 0.5669 0.4437 1.0106 1.0106 0.4608 0 0 0
0.2100 0.3250 1.1000 0.9750 0.1250 1.1000 0.4070 0 0 0
0.2048 0.4401 0.7116 1.1518 0.1521 0.2741 0 0 0 0
0.5575 0.2119 1.2212 0.3091 0.9121 0.8300 0.0993 0 0 0
0.6041 1.0239 0.6050 0.4189 1.0239 0.3428 0 0 0 0
0.5817 1.0205 0.4559 0.5645 1.0205 0.3417 0 0 0 0
0.9277 1.0209 0.5690 0.4519 1.0209 1.0209 0.4390 0 0 0
0.1817 1.0217 0.7974 0.2243 0.4393 0 0 0 0 0
0.4787 0.9174 0.2224 1.1398 0.7216 0.4182 0.3816 0 0 0
0.9091 1.0004 1.0004 1.0004 1.0004 0.5803 0 0 0 0
0.2126 1.0104 0.1849 1.1953 0.4451 0.2482 0 0 0 0
0.2531 0.2652 1.0657 1.0657 0.4962 0.5695 0.7034 0 0 0
0.2072 0.8844 0.2805 1.1650 0.5038 0.2651 0 0 0 0
0.3229 1.0774 0.0362 1.1136 1.1136 0.0092 0.3636 0 0 0
0.2172 1.2212 0.0671 1.1541 0.5882 0.2788 0 0 0 0
0.3084 1.0633 1.0633 0.3386 0.4706 0 0 0 0 0
0.2108 1.0037 1.0037 1.0037 0.3361 0 0 0 0 0
0.1521 0.0784 1.0078 1.0862 0.4669 0.6193 0.3637 0 0 0
0.9111 1.0026 1.0026 1.0026 1.0026 0.8622 0 0 0 0
0.1406 1.0046 1.0046 1.0046 1.0046 1.0046 1.0046 0.5075 0 0
0.1918 0.0487 1.0297 1.0784 0.5640 0.3634 0 0 0 0
0.1513 0.0229 1.2441 0.4140 0.8301 0.8509 0.0958 0 0 0
0.8749 1.0001 1.0001 0.0422 0 0 0 0 0 0
0.8900 1.0173 0.5089 0.5084 1.0173 1.0173 0.5565 0 0 0
0.8903 1.0177 0.7745 0 0 0 0 0 0 0
1.0498 0.0125 1.2351 0.4561 0.7790 0.4136 0 0 0 0
1.0150 0.7605 0.4198 0.8982 0 0 0 0 0 0
0.1808 1.0165 1.0165 1.0165 0.1931 0 0 0 0 0
0.8158 1.0071 0.2857 0.4807 0 0 0 0 0 0
0.8973 0.2251 0.9714 0.8502 0.3462 0.4006 0 0 0 0
0.2134 1.2000 0.1495 1.0506 0.7584 0.4417 0.3000 0 0 0
0.7503 1.0005 1.0005 1.0005 1.0005 0.5472 0 0 0 0
0.8519 1.1359 0.0001 0.8643 0 0 0 0 0 0
0.7315 0.4265 0.7546 0.3955 0 0 0 0 0 0
0.6675 0.8202 0 0 0 0 0 0 0 0
0.3814 1.0037 1.0037 1.0037 0.5490 0 0 0 0 0
0.3608 1.0934 0.0694 0.2967 0 0 0 0 0 0
0.1815 1.0204 1.0204 1.0204 1.0204 0.0804 0.6033 0 0 0
0.3523 1.0676 1.0676 0.0248 1.0429 0.5840 0 0 0 0
0.3721 0.6114 0.6717 0.4296 0 0 0 0 0 0
0.1866 1.0493 1.0493 0.5664 0.4830 1.0493 0.7450 0 0 0
0.2975 1.0260 0.0635 0.9624 1.0260 0.5612 0 0 0 0
0.3385 0.8884 0 0 0 0 0 0 0 0
0.2690 0.8727 0.3151 0.3977 0 0 0 0 0 0
0.2374 1.0482 0.7679 0.2802 1.0482 0.5733 0 0 0 0
0.1428 1.0986 0.2470 0.1208 0 0 0 0 0 0
0.1407 1.0821 1.0821 0.1822 0.8999 0.5919 0 0 0 0
0.1300 0.7610 0 0 0 0 0 0 0 0
0.9675 0.0053 0.4343 0 0 0 0 0 0 0
0.1817 1.0220 1.0220 1.0220 0.9418 0.0802 1.0220 0.2555 0 0
0.8054 1.0738 0.0971 0.9767 0.5874 0 0 0 0 0
0.7961 0.2463 0.5288 0 0 0 0 0 0 0
0.6895 0.6923 0.3524 1.0447 0.5715 0 0 0 0 0
0.6871 0 0 0 0 0 0 0 0 0
0.9155 0 0 0 0 0 0 0 0 0
0.4147 0 0 0 0 0 0 0 0 0
0.7308 0.0113 1.1596 0.8044 0.3522 0 0 0 0 0
0.6601 0.3453 0 0 0 0 0 0 0 0
0.5076 0.0782 0 0 0 0 0 0 0 0
0.7649 0.2744 0.5651 0 0 0 0 0 0 0
0.6115 0.3237 0.1214 0 0 0 0 0 0 0
0.7236 0.0776 0.6387 0 0 0 0 0 0 0
0.5311 0.1151 0.9963 0 0 0 0 0 0 0
0.1641 0.0599 0.9660 1.0259 0.5612 0 0 0 0 0
0.8965 0 0 0 0 0 0 0 0 0
0.6188 1.0043 0.1607 0 0 0 0 0 0 0
0.4533 0.3284 0.9263 0 0 0 0 0 0 0
0.3628 0.4568 0.4365 0 0 0 0 0 0 0
0.2558 0.5884 0 0 0 0 0 0 0 0
0.1884 0.4902 0 0 0 0 0 0 0 0
0.1455 0.0447 0.5526 0 0 0 0 0 0 0
0.0793 0.3838 0 0 0 0 0 0 0 0
0.0805 1.0149 0.6064 0 0 0 0 0 0 0
0.7523 0.8290 0.2982 1.1272 0.5694 0 0 0 0 0
0.1034 0.9373 0.3661 1.1931 0.1102 0.7427 0 0 0 0
0.0888 0.4431 0.6763 0.0638 0 0 0 0 0 0
0.0670 0.3839 0 0 0 0 0 0 0 0
0.0681 1.0164 0.6073 0 0 0 0 0 0 0
0.0755 0.4427 0.6843 0.0642 0 0 0 0 0 0
0.5619 0.4598 0.6869 1.1467 0.2045 0 0 0 0 0
0.4987 0.3906 0 0 0 0 0 0 0 0
0.5430 0.0114 1.0968 0.0632 0 0 0 0 0 0
0.4407 0.4027 0 0 0 0 0 0 0 0
0.4495 1.0963 0.2350 0.8613 0.5538 0 0 0 0 0
0.5038 0.4028 0 0 0 0 0 0 0 0
0.4943 0.0541 1.1424 0.0651 0 0 0 0 0 0
0.6954 0.4171 0 0 0 0 0 0 0 0
0.4677 0.2769 1.1634 0.0663 0 0 0 0 0 0
0.1589 1.1350 0.2493 0.8856 0.5733 0 0 0 0 0
0.3814 0.0163 1.2831 0.0731 0 0 0 0 0 0
0.6389 1.0369 0.4251 0 0 0 0 0 0 0
0.0710 1.0149 0.5551 0 0 0 0 0 0 0
0.4376 0.6881 1.0147 0 0 0 0 0 0 0
0.6393 1.1416 0.2036 0 0 0 0 0 0 0
0.3888 1.0800 0.1926 0 0 0 0 0 0 0
0.6852 0.3812 0 0 0 0 0 0 0 0
0.4217 0.7882 0.3831 0.6674 0 0 0 0 0 0
0.2469 1.0330 0.1550 0.2272 0 0 0 0 0 0
0.6003 0.1677 0.6004 0 0 0 0 0 0 0
0.3019 0.1893 1.0737 0.5634 0.0933 0 0 0 0 0
0.8692 0.1094 0.5962 0 0 0 0 0 0 0
0.3294 0.5862 0.7919 0.6614 0.0828 0 0 0 0 0
0.3616 1.1205 0.0095 0.5708 0 0 0 0 0 0
0.9761 0.2160 0.9891 0.4625 0 0 0 0 0 0
0.1902 1.0011 0.5057 0 0 0 0 0 0 0
0.2418 1.0338 1.0338 0.5295 0 0 0 0 0 0
0.2351 1.0051 0.1556 0.8495 0.4699 0 0 0 0 0
0.2517 1.0759 1.0233 0.0526 0.5968 0 0 0 0 0
0.2342 1.0014 1.0014 0.4238 0 0 0 0 0 0
0.2430 1.0389 0.9815 0.0574 0.4320 0 0 0 0 0
0.2707 0.5728 0.5845 1.1573 0.2449 0.2636 0 0 0 0
0.1065 0.1941 1.2852 0.1126 1.1726 0.4194 0.2113 0 0 0
0.5830 0.5932 0 0 0 0 0 0 0 0
0.4807 0.7002 0.5100 1.2102 0.0551 0.4094 0 0 0 0
0.8563 0.0122 0.5702 0 0 0 0 0 0 0
0.1487 0.9660 0.3856 0.3652 0 0 0 0 0 0
0.7246 0.1034 0.4980 0 0 0 0 0 0 0
0.2777 0.7889 0.3046 0 0 0 0 0 0 0
Columns 11 through 20
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I want to remove all the rows which are all zeros.
ThanksBOBhttps://jp.mathworks.com/matlabcentral/profile/authors/11029539-bobtag:jp.mathworks.com,2005:Question/339482012-03-30T04:12:20Z2020-03-29T01:27:08ZArduino InterruptsI wish to use the "Arduino Support for Simulink" to write code in Simulink and use it on an Arduino. But, I need to be able to incorporate interrupts in order to read the output from a quadrature encoder. Does Simulink support this? If not is there a way to access the sketch file before it is complied to add some more functionality that Simulink is lacking?Curtis Bowerhttps://jp.mathworks.com/matlabcentral/profile/authors/2829181-curtis-bowertag:jp.mathworks.com,2005:Question/5136582020-03-29T01:20:08Z2020-03-29T01:20:08Zwritematrix with Range has problem. Hello.
I am confusing while I am using 'writematrix'.
Here is the code have the problem.
for i=1:5
CellRange = ['A',num2str(i)];
writematrix(filename,'Sheet',1,'Range','CellRange')
end
I know the Range should be Character type.
for i=1:5
firstRow = i;
firstCol = 'C';
cellRange = [firstCol,num2str(firstRow)];
end
Result of cellRange is character type as I expected.
But when I applied this into upper code, it's error message said that 'The range must be a character vector of the form 'A1:B2' or 'A1''
Is there something adjust for this?
Thank you. Kyoungtak Kimhttps://jp.mathworks.com/matlabcentral/profile/authors/12674754-kyoungtak-kimtag:jp.mathworks.com,2005:Question/5136322020-03-28T20:28:51Z2020-03-29T01:17:55ZUnable to perform assignment because brace indexing is not supported for variables of this type.I am doing this code each frame of videos.
Plese let me know how to fix this error.
clear all
close all
%// read the video:
list = dir('*.avi')
% loop through the filenames in the list
for k = 1:length(list)
reader = VideoReader(list(k).name);
vid = {};
while hasFrame(reader)
vid{end+1} = readFrame(reader);
end
for i=1:25
fIdx(i) = i; %// do it for frame 1 ~ 25
frameGray{i} = rgb2gray(vid{fIdx(i)});
data{i} = frameGray{i}';
data{i} = double(data{i});
opt.init_num_secants = 180;
idx1{i} = random('unid',size(data{i}, 2), opt.init_num_secants, 1);
idx2{i} = random('unid',size(data{i}, 2), opt.init_num_secants, 1);
secants{i} = data{i}(:, idx1{i})-data{i}(:, idx2{i});
D1{i} = abs(secants{i});
numSecants = size(data{i},1);
options.landmarks = 1:180;
[geo{i}] = IsomapII(D1{i}, 'k', 3, options);
secants{i} = geo{i}/norm(geo{i},2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%Parameter setting
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
opt.outer_iterations = 1000;
switch 'l-bfgs'
case 'grad'
opt.linear_solver = 'grad';
opt.tau = 1e-1; % gradient step size
opt.inner_iterations = 10;
opt.beta1 = 1e-1; opt.beta2 = 1e-1; %penalty parameters
opt.eta1 = 1; opt.eta2 = 1; %lagrangian update
case 'cgs'
opt.linear_solver = 'cgs';
opt.linear_iterations = 10;
opt.inner_iterations = 1;
opt.beta1 = 1; opt.beta2 = 1; %penalty parameters
opt.eta1 = 1.618; opt.eta2 = 1.618; %lagrangian update
case 'l-bfgs'
opt.linear_solver = 'l-bfgs';
opt.linear_iterations = 3;
opt.lbfgs_rank = 5;
opt.inner_iterations = 1;
opt.beta1 = 1; opt.beta2 = 1; %penalty parameters
opt.eta1 = 1.618; opt.eta2 = 1.618; %lagrangian update
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%End parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta = 0.3; %max-margin parameter
funA = @(z) funA_secants_WY(z, secants{i});
funAT = @(z) funAT_secants_WY(z, secants{i});
b = ones(numSecants, 1);
ticID = tic;
[P{i}, L{i}, q{i}, Lambda{i}, w{i}] = NuMax(funA, funAT, b, delta, opt);
toc(ticID);
[U{i}, S{i}, V{i}] = svd(P{i});
r{i} = rank(P{i});
U1{i} = U{i}(:, 1:r{i});
U1{i} = (U1{i} - min(U1{i}(:)))/(max(U1{i}(:))-min(U1{i}(:)));
Phi_NuMax = (U{i}(:, 1:r{i})*(S{i}(1:r{i}, 1:r{i}).^(1/2)))';
data = data{i};
Y = Phi_NuMax * data;
end
end
X = cell2mat(Y');
Konghttps://jp.mathworks.com/matlabcentral/profile/authors/14262556-kongtag:jp.mathworks.com,2005:Question/5136502020-03-28T22:47:34Z2020-03-29T01:13:55Zhow to synchronize structures with date fields in text formatI have a 31x1 cell array C. Each cell of C contains a structure of variable dimensions (41x1 to 50x1) but all structures have the same number of fields. Each structure has a ‘date’ field. The dates in each ‘date’ field are in text format.
I want to transform each structure in C into a timetable so I can synchronize them. I tried struct2table and then table2timetable but it does not work because the dates are not in datetime format. How do I change the format of the dates in the ‘date” field of each structure, so I can convert the structures in timetables and then synchronize them?
Thank youGiovanni Barbarossahttps://jp.mathworks.com/matlabcentral/profile/authors/8731259-giovanni-barbarossatag:jp.mathworks.com,2005:Question/5136572020-03-29T01:10:58Z2020-03-29T01:10:58Z How to create a template that will evolve over time and compare the current detection with the previous one within a search area?Hi, I'm doing analysis on a set of images in which I detect a circle, which in most cases is bright and well distinguished. For certain experiments the circle takes on a color very similar to the background and detection becomes difficult. I wanted to know if there is a way to create a template that will evolve over time and compare the current detection with the previous one within a search area. This is because I detect more than 50,000 images and it is important not to have many errors.
Thank you very much.CVChttps://jp.mathworks.com/matlabcentral/profile/authors/14744288-cvctag:jp.mathworks.com,2005:Question/433972012-07-11T21:56:24Z2020-03-29T01:09:59ZPerforming Gauss Elimination with MatLabK =
-0.2106 0.4656 -0.4531 0.7106
-0.6018 0.2421 -0.8383 1.3634
0.0773 -0.5600 0.4168 -0.2733
0.7945 1.0603 1.5393 0.0098
I have the above matrix and I'd like to perform Gauss elimination on it with MatLab such that I am left with an upper triangular matrix. Please how can I proceed?Lukumon Kazeemhttps://jp.mathworks.com/matlabcentral/profile/authors/3550998-lukumon-kazeemtag:jp.mathworks.com,2005:Question/5136412020-03-28T21:18:06Z2020-03-29T01:01:51ZCopyfile/ movefile when filenames are not directly availableHello Everyone,
I have two .xlsx files in a folder, I want to copy/ move those .xlsx files to a different folder but the user is not aware of file names which are stored in a workspace variable of the same Matlab program.
As shown below: 'flagged' is a workspace variable of my program which has file names. I want to copy/ move those specific files to a separate folder.
I tried following but it's not working
copyfile flagged(i).name newFolder
Please advise.
Thanks!Afzal Mohammedhttps://jp.mathworks.com/matlabcentral/profile/authors/13451468-afzal-mohammedtag:jp.mathworks.com,2005:Question/5136522020-03-28T23:28:06Z2020-03-29T01:01:07ZSolving mach number for 5000 different iterationsI am trying to solve an equation for Mach number for the Rayleigh pitot tube equation over 5000 iterations for each time I acquired data.
%% Equations
%p02/p1 ratio
p02_p1_6 = p02_6./p1_6;
p02_p1_8 = p02_8./p1_8;
p02_p1_10 = p02_10./p1_10;
p02_p1_12 = p02_12./p1_12;
p02_p1_14 = p02_14./p1_14;
%% Find M with rayleigh's pitot tube formula
syms M6 M8 M10 M12 M14
g = 1.4;
for i = 1:5000
eqn1 = (((((g+1)^2)*M6^2)/(4*g*(M6^2)-2*(g-1)))^(g/g-1)*...
(1-g+2*g*M6^2)/(g+1))-p02_p1_6(i) == 0;
V6 = vpasolve(eqn1,M6);
eqn2 = (((((g+1)^2)*M8^2)/(4*g*(M8^2)-2*(g-1)))^(g/g-1)*...
(1-g+2*g*M8^2)/(g+1))-p02_p1_8(i) == 0;
V8 = vpasolve(eqn2,M8);
eqn3 = (((((g+1)^2)*M10^2)/(4*g*(M10^2)-2*(g-1)))^(g/g-1)*...
(1-g+2*g*M10^2)/(g+1))-p02_p1_10(i) == 0;
V10 = vpasolve(eqn3,M10);
eqn4 = (((((g+1)^2)*M12^2)/(4*g*(M12^2)-2*(g-1)))^(g/g-1)*...
(1-g+2*g*M12^2)/(g+1))-p02_p1_12(i) == 0;
V12 = vpasolve(eqn4,M12);
eqn5 = (((((g+1)^2)*M14^2)/(4*g*(M14^2)-2*(g-1)))^(g/g-1)*...
(1-g+2*g*M14^2)/(g+1))-p02_p1_14(i) == 0;
V14 = vpasolve(eqn5,M14);
end
It takes about 15 minutes to get an output but every time it returns symbolicsZachary Kubalahttps://jp.mathworks.com/matlabcentral/profile/authors/12107060-zachary-kubalatag:jp.mathworks.com,2005:Question/5136452020-03-28T21:50:53Z2020-03-29T00:57:37ZHow to show word from excel?I want to show the word from excel. But I don't know how to show it. This is my code.
clc;
clear all;
T=readtable('EVASUSANTIDATA.xlsx');
x1=T.PENDAFTARAN;
x2=T.TUNDA;
x3=T.KUOTA;
y=T.LAMA;
For example, I want to get this result:
KOTA KUOTA LAMA
KOTA BENGKULU 311 21
it's the data number one in excel. With 'KUOTA' is 'x3' and 'LAMA' is 'y' Eddy Iswardihttps://jp.mathworks.com/matlabcentral/profile/authors/16292322-eddy-iswarditag:jp.mathworks.com,2005:Question/5136552020-03-29T00:09:12Z2020-03-29T00:29:20Zcombine or merge multiple excel files into one file. hi, i have 100 excel files and they have one of the three following types.
type1:
name id 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
david a
james a
tome a
alim a
type2:
name id 2010 2011 2012 2013 2014 2015 2016 2017 2018
david a
james a
tome a
alim a
type3:
name id 2011 2012 2013 2014 2015 2016 2017 2018 2019
david a
james a
tome a
alim a
is there any way that i can combine all of the 100 files into one by using simple loop or any other feasible way?.
Thanksmirewuti muhetaerhttps://jp.mathworks.com/matlabcentral/profile/authors/8756226-mirewuti-muhetaertag:jp.mathworks.com,2005:Question/5135632020-03-28T12:36:33Z2020-03-29T00:12:57ZAdd multiply shapefiles in one figure and set custom colors for themHey all,
I have 4 shape file. I know how to read them using this code:
% first shape file is for whole country
Country = 'country.shp'
S_country = shaperead (country);
M_country = mapshow (S_country); % >>>> I want to be in a white with black border <<<< can you please tell me how to?
% Second shape file for a sea in the north
north_sea = 'caspian sea.shp'
S_north_sea = shaperead (north_sea);
M_north_sea = mapshow (S_country); % I want to be in blue with no border
% Third shape file for a gulf in the south
Sout_gulf = 'persiangulf.shp'
S_Sout_gulf = shaperead (Sout_gulf);
M_Sout_gulf = mapshow (S_Sout_gulf); % I want to be in blue with no border
% Fourth Shape file for a sea in the south
south_sea = 'oman sea.shp'
S_south_sea = shaperead (south_sea);
M_south_sea = mapshow (S_south_sea); % I want to be in blue with no border
But I don't know how to have all of them in the one figure to have a nicely figure of a country with sea in north and south with colors that I mentioed in the comments of code.
I attach all 4 shape file here.
Thank you. Behzad Navidihttps://jp.mathworks.com/matlabcentral/profile/authors/15525777-behzad-naviditag:jp.mathworks.com,2005:Question/5136542020-03-28T23:52:42Z2020-03-29T00:04:30ZHow can I properly index a table using isnan? (R2020a)I am writing multiple tables to an Excel spreadsheet, and do not want previously added tables to be overwritten. As such, I have created the following code. The variable R is a 14x11 table completely filled with NaN as my stated readtable range in Excel is currently empty. I have preceding code which uses manually specified filepaths to extract data from files and create the tables.
R = readtable('rangedata.xlsx',...
'Range','A2:K15',...
'ReadVariableNames',false)
if isnan(R(1,1))
writetable(T,'rangedata.xlsx','sheet',1,'Range','A2:B15');
elseif isnan(R(1,4))
writetable(T,'rangedata.xlsx','sheet',1,'Range','D2:E15');
elseif isnan(R(1,7))
writetable(T,'rangedata.xlsx','sheet',1,'Range','G2:H15');
else, isnan(R(1,10))
writetable(T,'rangedata.xlsx','sheet',1,'Range','J2:K15');
end
When running the script, I encounter the error "Check for missing argument or incorrect argument data type in call to function 'isnan'." I have tried changing all my index formatting to the following format, where Var1 is the name of the first variable, but that has not worked either.
if isnan(R(1,{'Var1'}))Ritchie Yuhttps://jp.mathworks.com/matlabcentral/profile/authors/17796482-ritchie-yutag:jp.mathworks.com,2005:Question/5136512020-03-28T23:08:52Z2020-03-28T23:34:49ZFunction running slow than scripts when placed in a 'for loop'?I am trying to execute a program which has a function being called in a for loop, say:
%version1
for i =1 : 1000
x = myfunc1(a,b,c);
y = myfunc2(d,e,f);
[m,n,o] = myfunc(x,y);
end
Now, when I code the same goal in a different way using scripts like:
%version2
for i =1 : 1000
x = a + b + c; %function is more complicated
y = d + e + f; %function is more complicated
m = x*y; %function is more complicated
n = x*x*y; %function is more complicated
o = x*y*y; %function is more complicated
end
Version 2 is 3x faster than version 1 while both give same values. Why is that happening? When to use scripts and when to use functions in a generic sense?
Thanks..Angshuman Podderhttps://jp.mathworks.com/matlabcentral/profile/authors/17472414-angshuman-poddertag:jp.mathworks.com,2005:Question/5136532020-03-28T23:30:45Z2020-03-28T23:32:03ZUse of Simulink output variables in Matlab. I am in the process of simulating a battery charger and want to make a matlab script based on the output variables of the simulation (power, current). How can i get Matlab to recognize these values producedjohnny msawbahhttps://jp.mathworks.com/matlabcentral/profile/authors/17290958-johnny-msawbahtag:jp.mathworks.com,2005:Question/5134492020-03-27T19:10:46Z2020-03-28T23:08:49Zwhat(): Unable to launch the MATLABWindow application during installationSo, I'm running a fresh install of Manjaro Gnome and I'm trying to install matlab. I downloaded the installer, unpacked but when I try to run "bash ./install" it gives me the error:
terminate called after throwing an instance of 'std::runtime_error'
what(): Unable to launch the MATLABWindow application
Matlab version is R2020a.Marcello Carbonihttps://jp.mathworks.com/matlabcentral/profile/authors/17920264-marcello-carbonitag:jp.mathworks.com,2005:Question/5136362020-03-28T21:02:46Z2020-03-28T23:05:29Zwhy do I get "Undefined function or variable 'tspan'."I'm trying to solve for pendulum and everytime I try to run the file I get
Undefined function or variable 'tspan'.
function [z_ydot] = pendulum(t,z_y,k,d,m,L,g)
z_ydot = zeros(4,1);
z_ydot(1) = z_y(2);
z_ydot(2) = (k.*d^2.*(z_y(3)-z_y(1))./(m.*L^2) - ((g.*z_y(1))./L);
z_ydot(3) = z_y(4);
z_ydot(4) = (k.*d^2.*(z_y(1)-z_y(3))./(m.*L^2) - ((g.*z_y(3))./L);
clear; clc; close all
g=9.81; % acceleration due to gravity
m=2; % mass in kilograms
L=0.75; % length in metres
d=0.5; % distance to the spring in metres
k=130; % spring stiffness in N/m
timespan = [0 20]; % timespan in seconds
% z1=theta; z2=thetadot
y_z0 = [0 0 13 0];
[t,z_y] = ode45(@(t,z_y) pendulum(t,z_y,k,d,m,L,g), tspan, y_z0);
% time history of theta1
figure
plot(t,z_y(:,1))
title('Theta 1');
xlabel('time')
ylabel('theta')
grind on
% time history of theta2
figure
plot(t,z_y(:,3))
title('Theta 2');
xlabel('time')
ylabel('thetadot')
grind onRaad Alsulaitihttps://jp.mathworks.com/matlabcentral/profile/authors/17551321-raad-alsulaititag:jp.mathworks.com,2005:Question/5136492020-03-28T22:44:02Z2020-03-28T22:58:18Zhow to obtain anonymous function after derivative ?Hi,
can we have an anonymous function after we differntiate an anonymous function? for example see blow
f = @(x) x^4+3*x^2;
now I want to have another anonymous function which is derivative of f(x) . Masoud Nateghihttps://jp.mathworks.com/matlabcentral/profile/authors/17939616-masoud-nateghitag:jp.mathworks.com,2005:Question/5134412020-03-27T18:25:26Z2020-03-28T22:57:28Zhow to fix this issue through running the program?i am using Matlab for medical image classification and i get this issue:
note: i used pre-trained network (alexnet) with .dicom files dataset.
first i prepare my design network
second, i run my code.
>> deepNetworkDesigner
>> SHIVANCLASSIFY
net =
SeriesNetwork with properties:
Layers: [25×1 nnet.cnn.layer.Layer]
InputNames: {'data'}
OutputNames: {'output'}
Error using trainNetwork (line 170)
The training images are of size 227x227x1 but the input layer expects images of size 227x227x3.
Error in SHIVANCLASSIFY (line 36)
net = trainNetwork(augimdsTrain,layers_1,options)
net=alexnet
imds = imageDatastore('lung dataset-Labeled', ...
'IncludeSubfolders',true, 'LabelSource','foldernames', ... % this for labeling by folder names
'FileExtensions','.dcm','ReadFcn',@readDicomDatastoreImage); % this a function
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);
augmenter = imageDataAugmenter( ...
'RandRotation',[-20,20], ...
'RandXReflection',1,...
'RandYReflection',1,...
'RandXTranslation',[-3 3], ...
'RandYTranslation',[-3 3]);
%augimdsTrain = augmentedImageDatastore([224 224],imdsTrain,'DataAugmentation',augmenter);
%augimdsValidation = augmentedImageDatastore([224 224],imdsValidation,'DataAugmentation',augmenter);
augimdsTrain = augmentedImageDatastore([227 227],imdsTrain);
augimdsValidation = augmentedImageDatastore([227 227],imdsValidation);
options = trainingOptions('rmsprop', ...
'MiniBatchSize',10, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-3, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(augimdsTrain,layers_1,options)
[YPred, probs] = classify(net,augimdsValidation);
accuracy = mean(YPred ==imdsValidation.Labels)
figure
cm=confusionchart (imdsValidation.Labels, YPred);shivan artoshhttps://jp.mathworks.com/matlabcentral/profile/authors/16817855-shivan-artoshtag:jp.mathworks.com,2005:Question/1158812014-02-12T18:53:25Z2020-03-28T22:54:50ZTo create a prediction model for road traffic time series data(i.e vehicle count)using artificial neural network in matlabI'm basically trying to develop a short term prediction model for road traffic time series data using artificial neural networks using matlab. I wish to know whether its possible to achieve the objective using matlab? And if it is possible,can someone please provide some code snippet in matlab(since i am a beginner in this field) which involves such kind of forecasting for any time series data so that i can understand how basically forecasting is done in matlab (using artificial neural networks concept)? I have the vehicle count of 5 days of a week of a particular road and wish to forecast the values of next 2 days of the week using the time series data.Hence i plan on having 5 input nodes, 5-10 neurons in hidden layer and 2 nodes in output layer as a part of my model. Any help in this regard would be highly appreciated.shripatihttps://jp.mathworks.com/matlabcentral/profile/authors/5057687-shripatitag:jp.mathworks.com,2005:Question/5135732020-03-28T13:30:15Z2020-03-28T22:54:09ZHow to create a distribution of the spread of the data from its meanHow can I create e distribution of the spread for the data below. I want to have in the same figure 5 distributions respresenting
the spread of the data from its mean in each zone.The columns contain the daily prices for 5 different zones.
My purpose is to check if there are significant differences in the prices between zones.
I have tried to use the following code but it is not fulfilling my purpose. Anyone can help me ...
x1=Data(:,1); x2=Data(:,2); x3=Data(:,3); x4=Data(:,4); x5=Data(:,5);
z1=(x1-mean(x1))/std(x1);
z2=(x2-mean(x2))/std(x2);
z3=(x3-mean(x3))/std(x3);
z4=(x4-mean(x4))/std(x4);
z5=(x5-mean(x5))/std(x5);
pd1=fitdist(z1,'Kernel');
pd2=fitdist(z2,'Kernel');
pd3=fitdist(z3,'Kernel');
pd4=fitdist(z4,'Kernel');
pd5=fitdist(z5,'Kernel');
s=(-2:0.01:2);
y1=pdf(pd1,s);
y2=pdf(pd2,s);
y3=pdf(pd3,s);
y4=pdf(pd4,s)
y5=pdf(pd4,s);;
figure
p=plot(s,y1,'-b',s,y2,'k',s,y3,'-g',s,y4,':r',s,y5,'-m');
title('Empirical kernel')
gjashtahttps://jp.mathworks.com/matlabcentral/profile/authors/12107468-gjashtatag:jp.mathworks.com,2005:Question/5136462020-03-28T22:02:18Z2020-03-28T22:52:27Z why does matlab tell me that the "chop" command doesn't exist?
why does matlab tell me that the chop command doesn't exist?Bodo Christianhttps://jp.mathworks.com/matlabcentral/profile/authors/17761812-bodo-christiantag:jp.mathworks.com,2005:Question/2824492016-05-04T00:35:20Z2020-03-28T22:51:41Zhow to stop ode45 when one of states reach certain value [x_dot] =derivative(t, x)
x is states [x(1).....x(4)]
I need to stop the integration when x(3) reaches 0.1
when the integration stop record (t)Mohamed Aburakhishttps://jp.mathworks.com/matlabcentral/profile/authors/5312036-mohamed-aburakhistag:jp.mathworks.com,2005:Question/5136442020-03-28T21:50:48Z2020-03-28T22:42:47ZArduino HC-05 HardwareHello guys,
I'm trying to Arduino and HC-05 connection. But have a problem and I don't why.
Writing command window 'arduinosetup' when came the configured BT Device Matlab give warning 'Failed to configured the Bluetooth device.Check the serial port and your device wiring. Someone please help me. Thanks. Have a nice works. Selen Armaganhttps://jp.mathworks.com/matlabcentral/profile/authors/17706354-selen-armagantag:jp.mathworks.com,2005:Question/4582352019-04-22T04:00:00Z2020-03-28T22:41:15ZWhy is the Canny edge detection in MATLAB different to OpenCV?When I am comparing the outputs of using the "edge" function in MATLAB to the "Canny" function in OpenCV, I notice that the OpenCV implementation filters out more edges compared to the MATLAB's implementation.
In MATLAB, I use:
>> edge(I, 'canny', [], 2.0)
In OpenCV (C++), I use:
>> cv::Canny(gray_image_smooth, edge, lowThresh, highThresh);
where I have tried to set the low and high thresholds using a manner similar to what is given in the "edge" function.
MathWorks Support Teamhttps://jp.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:jp.mathworks.com,2005:Question/5136482020-03-28T22:18:50Z2020-03-28T22:31:57ZHow can I add plot Err versus iBEYZA NUR ULUOGLUhttps://jp.mathworks.com/matlabcentral/profile/authors/17469188-beyza-nur-uluoglutag:jp.mathworks.com,2005:Question/70862011-05-09T14:59:59Z2020-03-28T22:14:42ZPlotting matrix columnsI was able to do this in the past but forgot how I did it. Starting with a 2D matrix I was able to plot it with the first column as the X axis and generate individual sub plots of the remanining columns. Each column was its own plot with the 1st column as the X axis. A loop could do this but I seem to remember it was a simple command that put multiple plots in one figure window.
Do I need to plot this as3D surface and then extract slices from the figure?
Markhttps://jp.mathworks.com/matlabcentral/profile/authors/2865573-marktag:jp.mathworks.com,2005:Question/5136472020-03-28T22:12:23Z2020-03-28T22:12:23ZSave a solution to a Matrix in SpecifyCoefficientsHi,
I am trying to save the solution of a PDE after running SpecifyCoefficients. I need to save it as a Matrix or as an interpolated function.
Is there any command for that?
Thank you,
A.HAli Hagverdiyevhttps://jp.mathworks.com/matlabcentral/profile/authors/8028550-ali-hagverdiyevtag:jp.mathworks.com,2005:Question/5136092020-03-28T18:09:53Z2020-03-28T22:07:58Zhow to add a single blank row in a tableDear collegues,
Supose my table is as follows
1 2 3
4 5 6
7 8 9
I want to convert it in:
1 2 3
4 5 6
7 8 9
That is, adding a blank row between some table rows. Can some one help me in this issue?
Thank you for you time.
Best,
AbdulAbdul Sulemanhttps://jp.mathworks.com/matlabcentral/profile/authors/3752762-abdul-sulemantag:jp.mathworks.com,2005:Question/5136372020-03-28T21:02:59Z2020-03-28T22:07:11ZData comparison and storingI've created a sensor to take readings daily .These readings change daily as it measures over a 24 hour period.
1) I need to code for it to compare these readings to its optimum levels of 40% - 50% for Humidity ,without hardcoding for specific values
2) To store only the values that exceed the threshold and the corresponding times these values are taken at in a 𝑛 ×3 vector
How do i go about doing this ?
Thanks
the data is stored as a seperate file whereby i call them
One example of readings are as follows :
Humidity =
20×1 string array
"34.099045"
"42.425098"
"33.480418"
"35.790796"
"30.370059"
"44.029928"
"49.041827"
"44.980187"
"45.134548"
"40.841782"
<missing>
<missing>
<missing>
<missing>
<missing>
<missing>
<missing>
<missing>
<missing>
<missing>
HumidityTime =
2 24
4 48
7 12
9 36
12 0
14 24
16 48
19 12
21 36
24 0
Zaahid Sulimanhttps://jp.mathworks.com/matlabcentral/profile/authors/14889395-zaahid-sulimantag:jp.mathworks.com,2005:Question/5126002020-03-24T10:08:48Z2020-03-28T21:49:45ZError using symengine in numerical solverHi guys
Im trying to calculate the Worst Case Expected returns according to Tüntücu and König (2004). In order to derive the robust weights, I need to solve an equation numerically. Below is the code for the calculation of the weights.
syms w_r
S = vpasolve(0==(1-((length(r_dc_s)^(-0.5))*(chi2inv(0.95,length(mu))))...
/(1*sqrt(w_r'*V*w_r)+((length(r_dc_s)^(-0.5))*(chi2inv(0.95,length(mu))))))...
*w1+w2-w_r,w_r);
w_r would be the robust weights in a vector of Nx1. When I run the code, I always get the following error message:
Error using symengine
Invalid operands.
Error in sym/privBinaryOp (line 1032)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in / (line 375)
X = privBinaryOp(A, B, 'symobj::mrdivide');
If I understand the error message correctly, I would have to use element-wise multiplication or division. I do not see though where this would make sense. It can be seen that only the first line of the equation would result in a 1x1 double object, so would the second. The third line contains only Nx1 vectors (w1 and w2 are the speculative and minimal weights). The only place where I can imagine it could make sense to perform a element-wise multiplication would be .*w1+w2-w_r. This leads to the same error message though..
Do you guys can help me out with this? Would be highly appreciated!
Thanks in advance,
DavidDavid Kellerhttps://jp.mathworks.com/matlabcentral/profile/authors/10085000-david-kellertag:jp.mathworks.com,2005:Question/5136422020-03-28T21:38:45Z2020-03-28T21:46:49ZTrying to put values from one array into anotherI've created the below array and want to take a value from it, divide it by the size of said array and then populate a row in a new array with that value.
So for the 1,1 value of the array (1) I want to divide by 5 (to give me a value of 0.2).
I then want to put this value in a new array, but populate the whole first row with it, like so:
0.2 0.2 0.2 0.2 0.2
I then want to repeat this the values (1,1 through 5,1) to give me a new array of:
0.2 0.2 0.2 0.2 0.2
2/5 2/5 2/5 2/5 2/5
3/5 3/5 3/5 3/5 3/5
4/5 4/5 4/5 4/5 4/5
1 1 1 1 1
I'm just not entirely sure how to do this, I thought maybe setting up a blank array of zeros would be a good start then add to that?
As well as this I then want to repeat the process in the horizontal direction i.e. for (1,1 through 1,5)
Thanks in advance
array = [1 1 1 1 1; 2 2 2 2 2; 3 3 3 3 3; 4 4 4 4 4; 5 5 5 5 5];
array
n=size(array,1);
n
blank=zeros(n,n);
figure
imshow(blank);
for i = 1:1:n
value=array(i,1);
value
divided=value/n;
divided
end
Adam Kerryhttps://jp.mathworks.com/matlabcentral/profile/authors/17484701-adam-kerrytag:jp.mathworks.com,2005:Question/5136432020-03-28T21:46:28Z2020-03-28T21:46:28Zstate-space modelI have a state-space models sys1 and sys2 with data (A1, B1, C1, D1) and (A2, B2, C2, D2) and i used append to create this space model:
how should the graph representation look and how can i do it in the matlab?Sebastian Badarauhttps://jp.mathworks.com/matlabcentral/profile/authors/17852676-sebastian-badarautag:jp.mathworks.com,2005:Question/2348862015-08-20T15:14:33Z2020-03-28T21:45:27Zzedboard fpga in loop Hello, Has anybody used zedboard in fpga in loop simulation? If so are there any documents or explainitions? Thank youiremhttps://jp.mathworks.com/matlabcentral/profile/authors/5339764-iremtag:jp.mathworks.com,2005:Question/3855902018-03-01T01:10:44Z2020-03-28T21:44:03ZHow do I complete my code to plot the Moody Chart? function [f] = frictionFactor(Re, ed) %Re = Reynolds Number, ed = eps/d, relative roughness
colebrook = @(f) 1/sqrt(f)+2*log10((ed/3.7)+(2.51)/(Re*sqrt(f)));
if Re > 4000 %turbulent
f = fzero(colebrook, [0.008, 0.1]);
elseif Re < 2000 %laminar
f = 64/Re;
else %transitional
f = (((Re-2000)/(4000-2000))*(0.1-0.008))+0.008;
end
array = linspace(0.000001, 0.05, 21);
for i = array
colebrook(i)
end
endSeanice Thompsonhttps://jp.mathworks.com/matlabcentral/profile/authors/12173450-seanice-thompsontag:jp.mathworks.com,2005:Question/3300212017-03-15T14:45:40Z2020-03-28T21:41:06Zpartial derivative and quotient rule and a product rulehi all i have a partial derivative which needs to be programmed on matlab.
struggling atm. help would be grateful.
m=(-10.4a-1.2b+1.2ab+28.8)(9.2ab-9.2ab^2)/(16b-16b^2+(7.1-2.3a)*(9.2a-9.2ab)+32.66ab-10.58ba^2)
we need to differentiate m with respect to a and b and equal it to zero.
we want to find out a and b when the above mentioned scenario is equal to zero.ehsan Zahoorhttps://jp.mathworks.com/matlabcentral/profile/authors/8854615-ehsan-zahoortag:jp.mathworks.com,2005:Question/5136052020-03-28T17:50:11Z2020-03-28T21:36:18Zerror message"Matrix index is out of range for deletion."Hello,
I get this error message when I run my program. I will be grateful if helping me to fix this issue
I have attached the code.Mojgan Soltanhttps://jp.mathworks.com/matlabcentral/profile/authors/11501277-mojgan-soltantag:jp.mathworks.com,2005:Question/5114932020-03-18T11:02:18Z2020-03-28T21:32:34ZArrayfun GPU in "Game of Life" works slower than CPUHello! I've run the demo "paralleldemo_gpu_stencil" and have such a result:
CPU: 2.815ms per generation.
Simple GPU: 2.650ms per generation (1.1x faster).
Arrayfun GPU: 13.253ms per generation (0.2x faster).
I've used ThinkPad P50 with Quadro M1000M and Matlab R2019b with appropriate drivers. Why Arrayfun works such slow ??
Uladzislau. Uladzislauhttps://jp.mathworks.com/matlabcentral/profile/authors/2689329-uladzislautag:jp.mathworks.com,2005:Question/5136252020-03-28T19:41:00Z2020-03-28T21:25:30ZHow to save a symbolic equation in a txt file?I have a complex system of equations for the dynamics of a rigid body.
I want to store the resultant matrix in a txt file (or any other format that works well) so that I do not have to compute the symbolic expression inside a numerical solver.
How do I store it in a proper format and then how do I read it?Anirudh Chhabrahttps://jp.mathworks.com/matlabcentral/profile/authors/12706986-anirudh-chhabratag:jp.mathworks.com,2005:Question/5136082020-03-28T18:09:12Z2020-03-28T21:17:11Z12.8 V, 40 Ah, Lithium-Ion (LiFePO4) Battery Aging Model (1000 h Simulation)Hello ,
I found this simulink project 12.8 V, 40 Ah, Lithium-Ion (LiFePO4) Battery Aging Model (1000 h Simulation which is very interesting for my thesis. But I cannot open the example at my matlab , i have matlab 201 6( all included simscape simulink etc ) and i tried the command power_battery_aging but i cannot open it .
Also i am trying to start the project at simulink at my own but i dont know what is included at the subsystem cycles generator
Can anyone help me please?
ANGELA TRhttps://jp.mathworks.com/matlabcentral/profile/authors/8926937-angela-trtag:jp.mathworks.com,2005:Question/5136402020-03-28T21:13:11Z2020-03-28T21:13:11Zroc not matching confusion matrix and error rate in fitcecocHi
mine error rate is 0 my confusion matris is 100% but ROC tellingdifferent story
i suppose my score is worng but please advice how to fix code
confusion matrix
ans =
532 0 0 0 0 0
0 491 0 0 0 0
0 0 537 0 0 0
0 0 0 496 0 0
0 0 0 0 420 0
0 0 0 0 0 471
code
%set up hyper parameters
Coding = 'onevsone';%CodingDesign
BC = 989.110; %Box
Ker = 12.059; %KernelScale
t = templateSVM( 'KernelFunction','gaussian','BoxConstraint',BC, 'KernelScale',Ker)
Mdl = fitcecoc(TrainSet,TargetSet,'Coding', Coding,'Learners',t);
% error rate
error = resubLoss(Mdl)
% xval = crossval(Mdl,'Kfold',10)
% kfoldLoss(xval)
%ROC
%sourc ehttps://uk.mathworks.com/help/stats/perfcurve.htmlC
[label,score] = resubPredict(Mdl)
%[label,NegLoss,PBScore] = predict(Mdl,TrainSet)
%plotROC
score = score(:,1)
[Xlog1,Ylog1,Tsvm1,AUC1] = perfcurve(TargetSet, score,1)
[Xlog2,Ylog2,Tsvm2,AUC2] = perfcurve(TargetSet, score,2)
[Xlog3,Ylog3,Tsvm3,AUC3] = perfcurve(TargetSet, score,3)
[Xlog4,Ylog4,Tsvm4,AUC4] = perfcurve(TargetSet, score,4)
[Xlog5,Ylog5,Tsvm5,AUC5] = perfcurve(TargetSet, score,5)
[Xlog6,Ylog6,Tsvm6,AUC6] = perfcurve(TargetSet, score,6)
hold on
plot(Xlog1,Ylog1)
plot(Xlog2,Ylog2)
plot(Xlog3,Ylog3)
plot(Xlog4,Ylog4)
plot(Xlog5,Ylog5)
plot(Xlog6,Ylog6)
hold off
%plot confusion
predictedY = resubPredict(Mdl)
confusionmat(TargetSet,predictedY)
%plotconfusion(TargetSet,predictedY)
Tomasz Kaczmarskihttps://jp.mathworks.com/matlabcentral/profile/authors/17173063-tomasz-kaczmarskitag:jp.mathworks.com,2005:Question/5136392020-03-28T21:11:00Z2020-03-28T21:11:00ZHow to classify using PCA ("pcares" function)Hello.
I want to do a video classification.
I found some function about PCA.
https://www.mathworks.com/help/stats/pcares.html
[residuals,reconstructed] = pcares(X,5)
After using pcares(X,5), Can I classify data using "reconstructed"?
I mean, "reconstructed" is 3600 x 25.
1. After flattening each data, I got 90000 X 1.
2. I have 90 data, so I got 90 X 90001 (the last column is class).
3. Using classification methods like SVM, KNN, etc.
4. calculate the accuracy of the classification.
Is it a reasonable approach? or do I need to use other methods as PCA not "pcares".
When I use "reconstructed" data after "pcares", the accuracy of classification is so low.
Please let me know how to deal with this issue.
Konghttps://jp.mathworks.com/matlabcentral/profile/authors/14262556-kongtag:jp.mathworks.com,2005:Question/5136282020-03-28T20:02:25Z2020-03-28T21:06:41ZHow to deploy my code on raspberry pi as a standalone?vid = videoinput('winvideo', 1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img)
h = findobj('type','figure');
n = length(h);
for k=1:n
baseFileName = sprintf('Img #%d.png', k);
fullFileName = fullfile('C:\Users\Cv\Desktop\image classification2 - copy',['img' '.bmp']);
imwrite(img, fullFileName);
end
outputFolder = fullfile('caltech102');
rootFolder = fullfile(outputFolder, '101_ObjectCategories');
categories = {'Bottles', 'NotBottles'};
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource', 'foldernames');
tb1 = countEachLabel(imds)
minSetCount = min(tb1{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
Bottles = find(imds.Labels == 'Bottles', 1);
NotBottles = find(imds.Labels == 'NotBottles', 1);
% figure
% subplot(2,2,1);
% imshow(readimage(imds,airplanes));
% subplot(2,2,2);
% imshow(readimage(imds,ferry));
% subplot(2,2,3);
% imshow(readimage(imds,laptop));
net = resnet50();
figure
plot(net)
title('Architecture of ResNet-50');
set(gca, 'YLim', [150 170]);
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize, ...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize, ...
testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
figure
montage(w1)
title('First Convolutional Layer Weight')
featureLayer = 'fc1000';
trainingFeatures = activations(net, ...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
trainingLables = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures,trainingLables, ...
'Learner', 'Linear', 'Coding', 'onevsall','ObservationsIn', 'columns');
testFeatures = activations(net, ...
augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn','columns');
testLables = testSet.Labels;
confMat = confusionmat(testLables,predictLabels);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat));
newImage = imread(fullfile('img.bmp'));
ds = augmentedImageDatastore(imageSize, ...
newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ...
ds, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
Label = predict(classifier, imageFeatures, 'ObservationsIn','columns');
sprintf('The Loaded image belongs to %s class', Label)
Ahmed abbasihttps://jp.mathworks.com/matlabcentral/profile/authors/14341848-ahmed-abbasitag:jp.mathworks.com,2005:Question/5136382020-03-28T21:04:26Z2020-03-28T21:04:26ZRicatti: applying control signal forward in timeHey all,
In an optimization class where we are learning about tracking to a desired output.
System
The system is a dc electric motor attached to a rotational load, with the s.s. described by:
x = [x1;x2;x3] = ang pos, vel, current || dx = [dx1;dx2;dx3] || A = [0 1 0; 0 0 4.438; 0 -12 -24] || B= [0;0;20]
There is a performance index, which I won't write, but R=0.01 and Q=[10 0 0;0 0 0;0 0 0]
output y is ang pos, tracking desired y_d(t) = 1
_________________________________________________________________________________________________________________
I was just auditing the class last term and am working through some HW's and have been stuck on applying the control signal forward in time.
I am NOT using the lqr function here; that yieds the steady state answers when T=infinity in the performance index. Here T=10, so the ricatti coefficients and co-state equations are solved first backwards in time, then applied to the control signal forward in time.
Here's what I've done (i.e. ^(T)=transpose):
Matlab I solved for the P matrix (defined as dP = PBR^(-1)B^(T)P - PA - A^(T)P - Q || P(T) = 0 )
Matlab I solved for the co state matrix (defined as db = -(A - BR^(-1)B^(T)P)^(T)*b + Q*x_d || b(T) = 0 )
I made a Simulink model which used gains/sums/integrators/froms/goto blocks to find p1,p2,...p6 and b1,b2,b3 from the above matrices
These succesfully gave me the steady state values (when looking at last of data set) of P from using [K,P,sig]=lqr(A,B,Q,R)
I also got the correct values for the co-state eq when using the Leibniz formula
Basically ^^^^ P and b are verified as correct!
What we know about u:
The optimal control at least, in a tracking problem, is defined as u*(t) = -R^(-1)B^(T)P(t)x(t) - R^(-1)B^(T)b(t)
You can see that the control signal changes in time due to P and b depending on time
In Matlab I managed to get a symbolic array of u*(t) with variables of x1,x2,x3 (since p1...p6 and b1..b3 are already known at each time step).
Problem:
Usually, I would go about this the same way as solving a s.s.
Using the s.s. equation I defined above, use gains/sums/integrators/froms/goto blocks and be fine. But that was when the control signal was constant because p1...p6 were constants.
Here they are changing with time.
I at first used a Matlab function block, where the function is simply y=u_opt_final (this contains the array of changing optimal control signals with time)
However it is not working:
Caused by:
Error in default port dimensions function of S-function 'a_control_out/MATLAB Function'. This function does not fully set the dimensions of output port 2
Additionally, Simulink wouldn't know how to step through an array for each time step in its simulation, anyway...
As you can see, I'm sort of stuck :/
I'm attaching my code and the model...if anyone would like to take an in-depth look at it.
OR, if anyone has better advice on how to go about this, I'm all ears. This is my first crack, there is no solution, and I haven't really seen anything online for the past week I have been working on it. At least, what I've seen is the steady state values, but again, that is not what I'm trying to do.
Thanks! That was a lot of info, and I hope any of it made sense, I am happy to clarify.
a_coefficients is parta of HW, where p1..p6 and b1..b3 are solved
a_control_out is where I am currently in a pickle
c_control_out is partc where T=inifinty, and the control signal is known b/c everything is constants. This is just to compare of a system that works
HW3.m is the main file
ChrisChris Dizonhttps://jp.mathworks.com/matlabcentral/profile/authors/14122415-chris-dizontag:jp.mathworks.com,2005:Question/5126822020-03-24T16:36:42Z2020-03-28T21:03:33ZDeep Learning - Distributed GPU Memory Hello,
I have many, very large input matrices (detector values) connected by Fully Conectet Layer and the output is a Regression Layer to reconstruct an image from it (Only 1 image is used at a time!). Due to the lack of local correlation, a Fully Connected Layer is necessary and CNN cannot be used. But this is beyond the VRAM.
Therefore the question if Matlab with Fully Connected can distribute this?
I have the choice to buy 2x rtx 8000 (2x48GB) or 4x Titan RTX (4x24GB). A RTX 8000 costs 2.5x as much as the Titan RTX and has only the same performance but with twice the memory of a Titan RTX.
2. NV-Link distribute GPU-RAM?
Thanks Daniel Grottkehttps://jp.mathworks.com/matlabcentral/profile/authors/15252848-daniel-grottketag:jp.mathworks.com,2005:Question/5136342020-03-28T20:53:46Z2020-03-28T21:01:59ZHow can I make a compound 3D geometry for pdemodel?I want to make a simple compound geometry to solve a pde, simply as two cuboids (neither of same height, nor of same cross section) joined together on a face. I tried several methods, but they fail in some cases. Is there a simple method to do so?
What I tried:
1) used K =boundary(X,Y,Z, lambda) to get triangular surface mesh, then geometryFromMesh(model, nodes, elements) to get the geometry but I am getting something as in figure(1). It has small fringes when lambda is around 0.995.
If this is the only possible method to do so, following are my doubts:
The boundary function gives a tight/loose enclousre covering all points in the point cloud. What points should I give to get least fringes? I am using the input as meshgrid of uniformly sampled points within the whole volume.
Should I use some other set of points to make the function give proper sharp triangulations (as there would be a sharp 90 degree boundary at junction of two cuboids)?
Is there any other triangulation technique to create surface mesh from point cloud, more accurately? I tried delaunayTraingulation, but it can't handle sharp features, and smoothens the sharp transition which is necessary.
I expected lambda = 1 to give the tightest non convex boundary hence making the edge sharp (rather than what I have shown in figure), but it makes the regular surfaces have sharp voids joining the points in below: it doesn't works.
Can I delete some faces or triangles (the ones at junction) from the obained geometry or triangulated mesh? I don't know how would I find the index.
(Sorry for so many questions, if any solution works, all other questions can be ignored :))
Figure description:
The small block is attached on face 7 of bigger block. Harshit Kshttps://jp.mathworks.com/matlabcentral/profile/authors/17467041-harshit-kstag:jp.mathworks.com,2005:Question/5135962020-03-28T16:59:46Z2020-03-28T20:59:33Z[c] = confusion(t,y) and not classified observationsHi
[c] = confusion(t,y)
[c] its Confusion value = fraction of samples misclassified
but output of code bellow can be values:
1)classified correctly
2)classified incorrectly
3)not classified
net = fitnet(hl,trainFcn)
[net,tr] = train(net,x,t);
y = net(x)
[c] = confusion(t,y)
if im getting c = 0 it mean all values were classified correctly but it will consider values which were not classified ?Tomasz Kaczmarskihttps://jp.mathworks.com/matlabcentral/profile/authors/17173063-tomasz-kaczmarskitag:jp.mathworks.com,2005:Question/5135992020-03-28T17:21:51Z2020-03-28T20:58:05ZRead text data and create array?I have a .txt file with ISS orbital parameters that I'm looking to import into Matlab and use to plot things like satellite position, for example.
I have successfully pulled the data from the website and printed to the attached .txt file but when I use readtable(), Matlab generates a 212x1 matrix instead of splitting the columns and creating a full array. Unfortunately, the format from the website hosting the orbit data features an unequal number of delimiters as well.
Any help and/or guidance on how to read this data, split it into columns, and create variables from it would be much appreciated. I do not need the string data (i.e. "ISS (ZARYA) ") from the top of each two row data set. Thanks.David Hamblinhttps://jp.mathworks.com/matlabcentral/profile/authors/17078262-david-hamblintag:jp.mathworks.com,2005:Question/444682012-07-25T04:49:13Z2020-03-28T20:57:33Zplotting a linear equationHow do I plot a linear equation y=mx+b?
So let's say I have this:
b0= 3 where b0 is the y-intercept
b1= 4 where b1 is the x coefficient
Then:
Y= b0-b1*X
How do I plot this?
Andyhttps://jp.mathworks.com/matlabcentral/profile/authors/3578982-andy