Category: Matlab
Category Archives: Matlab
Parse csv with complex numbers written by Python numpy
I am not actually sure which Python features use this format (I think both numpy and pandas use it), or perhaps this is a part of a larger standard, but basically I have csvs with text that looks like
(-0.0053973628685668375-0.004476730131734169j),(0.005108157082444198-0.005597795916657765j),,,,,,,-298.0,-298.0,37293,-0.7617709422297042,0.7202575393833991,(0.001506298444580933-0.0035885955125266656j)
and I want to parse into a numeric array.
The real-valued scalar entries are easy (well I can do a str2double and it’s not super fast but it’s acceptable). The blanks are also not too bad because after a simple textscan with a comma delimeter I can find emptys and set to a desired value. But what the heck do I do with these rediculous complex number strings?
There’s loopy solutions with regexp or finding the real and imag components but they are too slow when dealing with hundreds of thousands of entries. I could also do things like find entries containing a "j" and process them separately, but is there something better?I am not actually sure which Python features use this format (I think both numpy and pandas use it), or perhaps this is a part of a larger standard, but basically I have csvs with text that looks like
(-0.0053973628685668375-0.004476730131734169j),(0.005108157082444198-0.005597795916657765j),,,,,,,-298.0,-298.0,37293,-0.7617709422297042,0.7202575393833991,(0.001506298444580933-0.0035885955125266656j)
and I want to parse into a numeric array.
The real-valued scalar entries are easy (well I can do a str2double and it’s not super fast but it’s acceptable). The blanks are also not too bad because after a simple textscan with a comma delimeter I can find emptys and set to a desired value. But what the heck do I do with these rediculous complex number strings?
There’s loopy solutions with regexp or finding the real and imag components but they are too slow when dealing with hundreds of thousands of entries. I could also do things like find entries containing a "j" and process them separately, but is there something better? I am not actually sure which Python features use this format (I think both numpy and pandas use it), or perhaps this is a part of a larger standard, but basically I have csvs with text that looks like
(-0.0053973628685668375-0.004476730131734169j),(0.005108157082444198-0.005597795916657765j),,,,,,,-298.0,-298.0,37293,-0.7617709422297042,0.7202575393833991,(0.001506298444580933-0.0035885955125266656j)
and I want to parse into a numeric array.
The real-valued scalar entries are easy (well I can do a str2double and it’s not super fast but it’s acceptable). The blanks are also not too bad because after a simple textscan with a comma delimeter I can find emptys and set to a desired value. But what the heck do I do with these rediculous complex number strings?
There’s loopy solutions with regexp or finding the real and imag components but they are too slow when dealing with hundreds of thousands of entries. I could also do things like find entries containing a "j" and process them separately, but is there something better? csv, numpy, pandas, parsing MATLAB Answers — New Questions
how to calculate hourly averages?
I convertued my file to a csv, then used the following code to try and get hourly averages:
writematrix(NTU,’M.csv’);
T1 = readtable(‘M.csv’);
TT1 = table2timetable(T1,’RowTimes’,NTU);
TT1 = retime(TT1, ‘hourly’,’mean’);
Am I using table2timetable wrong?I convertued my file to a csv, then used the following code to try and get hourly averages:
writematrix(NTU,’M.csv’);
T1 = readtable(‘M.csv’);
TT1 = table2timetable(T1,’RowTimes’,NTU);
TT1 = retime(TT1, ‘hourly’,’mean’);
Am I using table2timetable wrong? I convertued my file to a csv, then used the following code to try and get hourly averages:
writematrix(NTU,’M.csv’);
T1 = readtable(‘M.csv’);
TT1 = table2timetable(T1,’RowTimes’,NTU);
TT1 = retime(TT1, ‘hourly’,’mean’);
Am I using table2timetable wrong? hourly, averages MATLAB Answers — New Questions
Unwrap with tolerance other than default (=pi)
I encountered a problem with the Matlab function “unwrap” when I tried to unwrap the phase of a signal with a jump tolerance other than pi.
I used the following phase for testing the behavior of the “unwrap” function:
phs = [0.1, 0.2, 0.3, 0.4, 0.4+pi-0.1, 0.4+pi-0.05, 0.4+pi-0.01];
There is a jump in the phase angle between elements four and five that is smaller than pi.
I then tried to eliminate this jump using the “unwrap” function and the tolerance pi/2:
phs = unwrap(phs, pi/2);
The phs vector didn’t change. It also didn’t change by using any other value for the tolerance.
Shouldn’t it eliminate the jump by adding +/- 2pi or by adding +/- pi/2?I encountered a problem with the Matlab function “unwrap” when I tried to unwrap the phase of a signal with a jump tolerance other than pi.
I used the following phase for testing the behavior of the “unwrap” function:
phs = [0.1, 0.2, 0.3, 0.4, 0.4+pi-0.1, 0.4+pi-0.05, 0.4+pi-0.01];
There is a jump in the phase angle between elements four and five that is smaller than pi.
I then tried to eliminate this jump using the “unwrap” function and the tolerance pi/2:
phs = unwrap(phs, pi/2);
The phs vector didn’t change. It also didn’t change by using any other value for the tolerance.
Shouldn’t it eliminate the jump by adding +/- 2pi or by adding +/- pi/2? I encountered a problem with the Matlab function “unwrap” when I tried to unwrap the phase of a signal with a jump tolerance other than pi.
I used the following phase for testing the behavior of the “unwrap” function:
phs = [0.1, 0.2, 0.3, 0.4, 0.4+pi-0.1, 0.4+pi-0.05, 0.4+pi-0.01];
There is a jump in the phase angle between elements four and five that is smaller than pi.
I then tried to eliminate this jump using the “unwrap” function and the tolerance pi/2:
phs = unwrap(phs, pi/2);
The phs vector didn’t change. It also didn’t change by using any other value for the tolerance.
Shouldn’t it eliminate the jump by adding +/- 2pi or by adding +/- pi/2? unwrap, tolerance, tol, jump, fish MATLAB Answers — New Questions
eliminate phase jumps in unwrapping
Hi everyone. I’m unwrapping the phase of a hemisphere image, using MATLAB unwrap function. the image contains vertical lines. the function ‘unwrap’ works well for a constant surface but it doesn’t for the hemisphere. there are some jumps in the unwrapped image.
I use the function in the form of:
UnwrappedImage7=unwrap(unwrap(PHI,[],2),[],1);
Do you have any sugestions to eliminate the jumps?Hi everyone. I’m unwrapping the phase of a hemisphere image, using MATLAB unwrap function. the image contains vertical lines. the function ‘unwrap’ works well for a constant surface but it doesn’t for the hemisphere. there are some jumps in the unwrapped image.
I use the function in the form of:
UnwrappedImage7=unwrap(unwrap(PHI,[],2),[],1);
Do you have any sugestions to eliminate the jumps? Hi everyone. I’m unwrapping the phase of a hemisphere image, using MATLAB unwrap function. the image contains vertical lines. the function ‘unwrap’ works well for a constant surface but it doesn’t for the hemisphere. there are some jumps in the unwrapped image.
I use the function in the form of:
UnwrappedImage7=unwrap(unwrap(PHI,[],2),[],1);
Do you have any sugestions to eliminate the jumps? unwrap, phase jump, image processing, image MATLAB Answers — New Questions
how to align two columns with different times
Hi, I have to align two timestamps, but I need help!
Input data: ML
the first two columns refer to the events codes (column 1) and the corresponding timestamps (column 2, in millieconds) from a time 0, of when each events occurs
ML (:,1)) = (events codes)
9 62 15 40 54 50 18 9 63 15 40 54 50 18 9 64 15 40 54 50 18 9 65 15 40 54 50 18 9 66 15 40 18 9 67 15 40 54 50 18 9 68 15 40 54 50 18 9 69 15 40 54 50 18 9 70 15 40 54 50 18 9 71 15 40 54 50 18 9 72 15 40 54 50 18 9 73 15 40 54 50 18 9 74 15 40 54 50 18 9 75 15 40 54 50 18 9 76 15 40 54 50 18 9 77 15 40 54 50 18 9 78 15 40 54 50 18 9 79 15 40 54 50 18 9 80 15 40 54 50 18 9 81 15 40 54 50 18 ;
ML(:,2) = ( timestamp 1, milliseconds of the times of each code from 0)
4974 4980 5112 5579 5968 6042 6374 6877 6882 6912 7545 8066 8133 8419 8921 8925 9012 9612 10115 10247 10533 11036 11040 11112 11679 12082 12102 12393 12896 12899 12979 13445 16592 17095 17098 17179 17545 17917 18000 18307 18809 18813 18913 19645 20148 20163 20462 20964 20968 21079 21612 21999 22065 22422 22926 22930 23112 23613 24010 24081 24384 24888 24893 24912 25379 25946 26006 26420 26922 26925 27112 27612 27991 28075 28368 28870 28873 28979 29546 30045 30061 30374 30875 30878 30946 31512 32029 32090 32349 32851 32854 33046 33712 34107 34164 34487 34988 34991 35046 35579 36093 36165 36460 36962 36965 37046 37446 37843 37919 38211 38712 38715 38780 39279 39655 39672 39975 40477 40479 40580 41146 41691 41753 42002 42504 42507 42613 43146 43657 43728 44019 44520 44523 44579 45180 45576 45641 45936
The third column is a separate time stamp (timestamp 2) , wtih the time when the codes40 occur from a time 0, relative to THAT timestamp (not to the timestamp 1) . The absolute values are different. In this timestamp, there are only the value of the code 40. The zeroes values correspond to a NaN.
ML(:,3)
0 0 0 1989 0 0 0 0 0 0 3464 0 0 0 0 0 0 5014 0 0 0 0 0 0 6564 0 0 0 0 0 0 7889 0 0 0 0 10964 0 0 0 0 0 0 12539 0 0 0 0 0 0 14014 0 0 0 0 0 0 15514 0 0 0 0 0 0 16839 0 0 0 0 0 0 18514 0 0 0 0 0 0 19964 0 0 0 0 0 0 21439 0 0 0 0 0 0 23089 0 0 0 0 0 0 24489 0 0 0 0 0 0 25889 0 0 0 0 0 0 27265 0 0 0 0 0 0 28665 0 0 0 0 0 0 30165 0 0 0 0 0 0 31690 0 0 0
even thought the absolute values of the two timestamps are different, nonetheless, the difference between two consecutive times of codes 40 in the column 2, should be the same as the difference bettween the consecutive times of code s 40 in colum 3. If this is the case, I can fill the zeroes value of the column 3, and obtain a complete timestamp of column 3 (timestamp 2) which correspond to the event codes:
%% select only the values which correspond to the code 40
gt = find(ML(:,1) == 40;
new_ML = ML(gt,:);
%% differences between times of consecutive codes 40:
timestamp1_diff = new_ML(2:end-1,2) – new_ML(1:end-1,2)
timestamp2_diff = new_ML(2:end-1,3) – new_ML(1:end-1,3)
However, the values of timestamp1_diff and timestamp2_diff are not the same. The values of the timestamp1_diff are alwasy between 400 and 550 bigger than the timestamp2_diff That means, that the values of the column 3 do not correspond to the code 40. I have to find to which code the values of the timestamp2 (ML:,3) correspond.
I tried, but I gave up!
any idea??
thanks
AnnaHi, I have to align two timestamps, but I need help!
Input data: ML
the first two columns refer to the events codes (column 1) and the corresponding timestamps (column 2, in millieconds) from a time 0, of when each events occurs
ML (:,1)) = (events codes)
9 62 15 40 54 50 18 9 63 15 40 54 50 18 9 64 15 40 54 50 18 9 65 15 40 54 50 18 9 66 15 40 18 9 67 15 40 54 50 18 9 68 15 40 54 50 18 9 69 15 40 54 50 18 9 70 15 40 54 50 18 9 71 15 40 54 50 18 9 72 15 40 54 50 18 9 73 15 40 54 50 18 9 74 15 40 54 50 18 9 75 15 40 54 50 18 9 76 15 40 54 50 18 9 77 15 40 54 50 18 9 78 15 40 54 50 18 9 79 15 40 54 50 18 9 80 15 40 54 50 18 9 81 15 40 54 50 18 ;
ML(:,2) = ( timestamp 1, milliseconds of the times of each code from 0)
4974 4980 5112 5579 5968 6042 6374 6877 6882 6912 7545 8066 8133 8419 8921 8925 9012 9612 10115 10247 10533 11036 11040 11112 11679 12082 12102 12393 12896 12899 12979 13445 16592 17095 17098 17179 17545 17917 18000 18307 18809 18813 18913 19645 20148 20163 20462 20964 20968 21079 21612 21999 22065 22422 22926 22930 23112 23613 24010 24081 24384 24888 24893 24912 25379 25946 26006 26420 26922 26925 27112 27612 27991 28075 28368 28870 28873 28979 29546 30045 30061 30374 30875 30878 30946 31512 32029 32090 32349 32851 32854 33046 33712 34107 34164 34487 34988 34991 35046 35579 36093 36165 36460 36962 36965 37046 37446 37843 37919 38211 38712 38715 38780 39279 39655 39672 39975 40477 40479 40580 41146 41691 41753 42002 42504 42507 42613 43146 43657 43728 44019 44520 44523 44579 45180 45576 45641 45936
The third column is a separate time stamp (timestamp 2) , wtih the time when the codes40 occur from a time 0, relative to THAT timestamp (not to the timestamp 1) . The absolute values are different. In this timestamp, there are only the value of the code 40. The zeroes values correspond to a NaN.
ML(:,3)
0 0 0 1989 0 0 0 0 0 0 3464 0 0 0 0 0 0 5014 0 0 0 0 0 0 6564 0 0 0 0 0 0 7889 0 0 0 0 10964 0 0 0 0 0 0 12539 0 0 0 0 0 0 14014 0 0 0 0 0 0 15514 0 0 0 0 0 0 16839 0 0 0 0 0 0 18514 0 0 0 0 0 0 19964 0 0 0 0 0 0 21439 0 0 0 0 0 0 23089 0 0 0 0 0 0 24489 0 0 0 0 0 0 25889 0 0 0 0 0 0 27265 0 0 0 0 0 0 28665 0 0 0 0 0 0 30165 0 0 0 0 0 0 31690 0 0 0
even thought the absolute values of the two timestamps are different, nonetheless, the difference between two consecutive times of codes 40 in the column 2, should be the same as the difference bettween the consecutive times of code s 40 in colum 3. If this is the case, I can fill the zeroes value of the column 3, and obtain a complete timestamp of column 3 (timestamp 2) which correspond to the event codes:
%% select only the values which correspond to the code 40
gt = find(ML(:,1) == 40;
new_ML = ML(gt,:);
%% differences between times of consecutive codes 40:
timestamp1_diff = new_ML(2:end-1,2) – new_ML(1:end-1,2)
timestamp2_diff = new_ML(2:end-1,3) – new_ML(1:end-1,3)
However, the values of timestamp1_diff and timestamp2_diff are not the same. The values of the timestamp1_diff are alwasy between 400 and 550 bigger than the timestamp2_diff That means, that the values of the column 3 do not correspond to the code 40. I have to find to which code the values of the timestamp2 (ML:,3) correspond.
I tried, but I gave up!
any idea??
thanks
Anna Hi, I have to align two timestamps, but I need help!
Input data: ML
the first two columns refer to the events codes (column 1) and the corresponding timestamps (column 2, in millieconds) from a time 0, of when each events occurs
ML (:,1)) = (events codes)
9 62 15 40 54 50 18 9 63 15 40 54 50 18 9 64 15 40 54 50 18 9 65 15 40 54 50 18 9 66 15 40 18 9 67 15 40 54 50 18 9 68 15 40 54 50 18 9 69 15 40 54 50 18 9 70 15 40 54 50 18 9 71 15 40 54 50 18 9 72 15 40 54 50 18 9 73 15 40 54 50 18 9 74 15 40 54 50 18 9 75 15 40 54 50 18 9 76 15 40 54 50 18 9 77 15 40 54 50 18 9 78 15 40 54 50 18 9 79 15 40 54 50 18 9 80 15 40 54 50 18 9 81 15 40 54 50 18 ;
ML(:,2) = ( timestamp 1, milliseconds of the times of each code from 0)
4974 4980 5112 5579 5968 6042 6374 6877 6882 6912 7545 8066 8133 8419 8921 8925 9012 9612 10115 10247 10533 11036 11040 11112 11679 12082 12102 12393 12896 12899 12979 13445 16592 17095 17098 17179 17545 17917 18000 18307 18809 18813 18913 19645 20148 20163 20462 20964 20968 21079 21612 21999 22065 22422 22926 22930 23112 23613 24010 24081 24384 24888 24893 24912 25379 25946 26006 26420 26922 26925 27112 27612 27991 28075 28368 28870 28873 28979 29546 30045 30061 30374 30875 30878 30946 31512 32029 32090 32349 32851 32854 33046 33712 34107 34164 34487 34988 34991 35046 35579 36093 36165 36460 36962 36965 37046 37446 37843 37919 38211 38712 38715 38780 39279 39655 39672 39975 40477 40479 40580 41146 41691 41753 42002 42504 42507 42613 43146 43657 43728 44019 44520 44523 44579 45180 45576 45641 45936
The third column is a separate time stamp (timestamp 2) , wtih the time when the codes40 occur from a time 0, relative to THAT timestamp (not to the timestamp 1) . The absolute values are different. In this timestamp, there are only the value of the code 40. The zeroes values correspond to a NaN.
ML(:,3)
0 0 0 1989 0 0 0 0 0 0 3464 0 0 0 0 0 0 5014 0 0 0 0 0 0 6564 0 0 0 0 0 0 7889 0 0 0 0 10964 0 0 0 0 0 0 12539 0 0 0 0 0 0 14014 0 0 0 0 0 0 15514 0 0 0 0 0 0 16839 0 0 0 0 0 0 18514 0 0 0 0 0 0 19964 0 0 0 0 0 0 21439 0 0 0 0 0 0 23089 0 0 0 0 0 0 24489 0 0 0 0 0 0 25889 0 0 0 0 0 0 27265 0 0 0 0 0 0 28665 0 0 0 0 0 0 30165 0 0 0 0 0 0 31690 0 0 0
even thought the absolute values of the two timestamps are different, nonetheless, the difference between two consecutive times of codes 40 in the column 2, should be the same as the difference bettween the consecutive times of code s 40 in colum 3. If this is the case, I can fill the zeroes value of the column 3, and obtain a complete timestamp of column 3 (timestamp 2) which correspond to the event codes:
%% select only the values which correspond to the code 40
gt = find(ML(:,1) == 40;
new_ML = ML(gt,:);
%% differences between times of consecutive codes 40:
timestamp1_diff = new_ML(2:end-1,2) – new_ML(1:end-1,2)
timestamp2_diff = new_ML(2:end-1,3) – new_ML(1:end-1,3)
However, the values of timestamp1_diff and timestamp2_diff are not the same. The values of the timestamp1_diff are alwasy between 400 and 550 bigger than the timestamp2_diff That means, that the values of the column 3 do not correspond to the code 40. I have to find to which code the values of the timestamp2 (ML:,3) correspond.
I tried, but I gave up!
any idea??
thanks
Anna align timestamps MATLAB Answers — New Questions
Error in calculating path to workspace goal region in robot’s path planning algorithm
Hi there, I’m having an issue in calculating a path for my rigid body tree object to follow to the specified goal region. I’m developing a script to find the path from from the home position of the robot to a specified goal region – much like the "Plan Path to a Workspace Goal Region Example" demonstrated in the manipulatorRRT documentation page. My issue specifically is that the plan function can not find a suitable path (outputing IsPathFound = 0, and ExitFlag = 2 – meaning that the maximum number of iterations was reached without finding a suitable path).
I have been following the prevously cited MATLAB example nearly word-for-word and my inputs to the plan function (the rrt object, the initial robot configuration, and the goal region) all function correctly and are passed without any errors.
I checked the following areas to try to fix this issue:
The robot (the rrt object) has the joint position limits set to [-inf, inf] and therefore can be rotated manually through the script to any angle – I believe that the joints have available freedom to rototate during the pathing calculations.
I have placed the goal region very close to the final link and set the bounds to have a very large positional and angular tolerance – I believe this should clear up any issues with difficult to reach angles.
Perhaps my lack of an end effector is affecting the pathing algorithm but every link (and frame of each link) resides in the goal region.
I have attached the URDF file and the smallest working example of the code as a .zip file to demonstrate the issue I’m having. Any help and/or advice would be greatly appreciated.
Thank you, Scott Brown.
Cal Poly SLO – Mechanical Engineering Master’s StudentHi there, I’m having an issue in calculating a path for my rigid body tree object to follow to the specified goal region. I’m developing a script to find the path from from the home position of the robot to a specified goal region – much like the "Plan Path to a Workspace Goal Region Example" demonstrated in the manipulatorRRT documentation page. My issue specifically is that the plan function can not find a suitable path (outputing IsPathFound = 0, and ExitFlag = 2 – meaning that the maximum number of iterations was reached without finding a suitable path).
I have been following the prevously cited MATLAB example nearly word-for-word and my inputs to the plan function (the rrt object, the initial robot configuration, and the goal region) all function correctly and are passed without any errors.
I checked the following areas to try to fix this issue:
The robot (the rrt object) has the joint position limits set to [-inf, inf] and therefore can be rotated manually through the script to any angle – I believe that the joints have available freedom to rototate during the pathing calculations.
I have placed the goal region very close to the final link and set the bounds to have a very large positional and angular tolerance – I believe this should clear up any issues with difficult to reach angles.
Perhaps my lack of an end effector is affecting the pathing algorithm but every link (and frame of each link) resides in the goal region.
I have attached the URDF file and the smallest working example of the code as a .zip file to demonstrate the issue I’m having. Any help and/or advice would be greatly appreciated.
Thank you, Scott Brown.
Cal Poly SLO – Mechanical Engineering Master’s Student Hi there, I’m having an issue in calculating a path for my rigid body tree object to follow to the specified goal region. I’m developing a script to find the path from from the home position of the robot to a specified goal region – much like the "Plan Path to a Workspace Goal Region Example" demonstrated in the manipulatorRRT documentation page. My issue specifically is that the plan function can not find a suitable path (outputing IsPathFound = 0, and ExitFlag = 2 – meaning that the maximum number of iterations was reached without finding a suitable path).
I have been following the prevously cited MATLAB example nearly word-for-word and my inputs to the plan function (the rrt object, the initial robot configuration, and the goal region) all function correctly and are passed without any errors.
I checked the following areas to try to fix this issue:
The robot (the rrt object) has the joint position limits set to [-inf, inf] and therefore can be rotated manually through the script to any angle – I believe that the joints have available freedom to rototate during the pathing calculations.
I have placed the goal region very close to the final link and set the bounds to have a very large positional and angular tolerance – I believe this should clear up any issues with difficult to reach angles.
Perhaps my lack of an end effector is affecting the pathing algorithm but every link (and frame of each link) resides in the goal region.
I have attached the URDF file and the smallest working example of the code as a .zip file to demonstrate the issue I’m having. Any help and/or advice would be greatly appreciated.
Thank you, Scott Brown.
Cal Poly SLO – Mechanical Engineering Master’s Student matlab, robotics, rigid body tree, pathing, rrt, path, robotics_toolbox, path planning, path_planning, workspace goal region, workspace_goal_region MATLAB Answers — New Questions
How to check collision between several robots?
I have created four robots (rigidBodyTrees) with the Robotics Toolbox.
In reality, this is one robot with four arms.
I chose to create four different robots, because I want to ignore self collision (the self being the same arm).
I want to generate configurations for each arm (each robot) and then check whether there is collision between them.
How do I do this? The documentation says:
checkCollision(robot,config,worldObjects)
This suggests that I can only enter the configuration for one robot and the worldObjects cannot be placed depending on the configuration that is generated but are ‘fixed’.
I really hope someone can help me!I have created four robots (rigidBodyTrees) with the Robotics Toolbox.
In reality, this is one robot with four arms.
I chose to create four different robots, because I want to ignore self collision (the self being the same arm).
I want to generate configurations for each arm (each robot) and then check whether there is collision between them.
How do I do this? The documentation says:
checkCollision(robot,config,worldObjects)
This suggests that I can only enter the configuration for one robot and the worldObjects cannot be placed depending on the configuration that is generated but are ‘fixed’.
I really hope someone can help me! I have created four robots (rigidBodyTrees) with the Robotics Toolbox.
In reality, this is one robot with four arms.
I chose to create four different robots, because I want to ignore self collision (the self being the same arm).
I want to generate configurations for each arm (each robot) and then check whether there is collision between them.
How do I do this? The documentation says:
checkCollision(robot,config,worldObjects)
This suggests that I can only enter the configuration for one robot and the worldObjects cannot be placed depending on the configuration that is generated but are ‘fixed’.
I really hope someone can help me! robot, collision checking, rigid body tree MATLAB Answers — New Questions
Why are the gradients not backpropagating into the encoder in this custom loop?
I am building a convolutional autoencoder using a custom training loop. When I attempt to reconstruct the images, the network’s output degenerates to guessing the same incorrect value for all inputs. However, training the autoencoder in a single stack with the trainnet function works fine, indicating that the gradient updates are unable to bridge the bottleneck layer in the custom training loop. Unfortunately, I need to use the custom training loop for a different task and am prohibited from using TensorFlow or PyTorch.
What is the syntax to ensure that the encoder is able to update based on the decoder’s reconstruction performance?
%% Functional ‘trainnet’ loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
aeLayers = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
autoencoder = dlnetwork(aeLayers);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
options = trainingOptions("adam", …
InitialLearnRate=learnRate,…
MaxEpochs=30, …
Plots="training-progress", …
TargetDataFormats="SSCB", …
InputDataFormats="SSCB", …
MiniBatchSize=miniBatchSize, …
OutputNetwork="last-iteration", …
Shuffle="every-epoch");
autoencoder = trainnet(dlarray(xTrain, ‘SSCB’),dlarray(xTrain, ‘SSCB’), …
autoencoder, ‘mse’, options);
%% Testing
YTest = predict(autoencoder, dlarray(xTest, ‘SSCB’));
indices = randi(size(xTest, 4), [1, size(xTest, 4)]); % Shuffle YTest & xTest
xTest = xTest(:,:,:,indices); YTest = YTest(:,:,:,indices);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = extractdata(YTest(:,:,:,1:numImages));
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Nonfunctional Custom Training Loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Encoder
layersE = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer];
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
layersD = [
imageInputLayer(projectionSize)
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
netE = dlnetwork(layersE);
netD = dlnetwork(layersD);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
% Create training minibatchqueue
dsTrain = arrayDatastore(xTrain,IterationDimension=4);
numOutputs = 1;
mbq = minibatchqueue(dsTrain,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFormat="SSCB", …
MiniBatchFcn=@preprocessMiniBatch,…
PartialMiniBatch="return");
%Initialize the parameters for the Adam solver.
trailingAvgE = [];
trailingAvgSqE = [];
trailingAvgD = [];
trailingAvgSqD = [];
%Calculate the total number of iterations for the training progress monitor
numIterationsPerEpoch = ceil(size(xTrain, 4) / miniBatchSize);
numIterations = numEpochs * numIterationsPerEpoch;
epoch = 0;
iteration = 0;
%Initialize the training progress monitor.
monitor = trainingProgressMonitor( …
Metrics="TrainingLoss", …
Info=["Epoch", "LearningRate"], …
XLabel="Iteration");
%% Training
while epoch < numEpochs && ~monitor.Stop
epoch = epoch + 1;
% Shuffle data.
shuffle(mbq);
% Loop over mini-batches.
while hasdata(mbq) && ~monitor.Stop
% Assess validation criterion
iteration = iteration + 1;
% Read mini-batch of data.
X = next(mbq);
% Evaluate loss and gradients.
[loss,gradientsE,gradientsD] = dlfeval(@modelLoss,netE,netD,X);
% Update learnable parameters.
[netE,trailingAvgE,trailingAvgSqE] = adamupdate(netE, …
gradientsE,trailingAvgE,trailingAvgSqE,iteration,learnRate);
[netD, trailingAvgD, trailingAvgSqD] = adamupdate(netD, …
gradientsD,trailingAvgD,trailingAvgSqD,iteration,learnRate);
updateInfo(monitor, …
LearningRate=learnRate, …
Epoch=string(epoch) + " of " + string(numEpochs));
recordMetrics(monitor,iteration, …
TrainingLoss=loss);
monitor.Progress = 100*iteration/numIterations;
end
end
%% Testing
dsTest = arrayDatastore(xTest,IterationDimension=4);
numOutputs = 1;
ntest = size(xTest, 4);
indices = randi(ntest,[1,ntest]);
xTest = xTest(:,:,:,indices);% Shuffle test data
mbqTest = minibatchqueue(dsTest,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFcn=@preprocessMiniBatch, …
MiniBatchFormat="SSCB");
YTest = modelPredictions(netE,netD,mbqTest);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = YTest(:,:,:,1:numImages);
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Functions
function [loss,gradientsE,gradientsD] = modelLoss(netE,netD,X)
% Forward through encoder.
Z = forward(netE,X);
% Forward through decoder.
Xrecon = forward(netD,Z);
% Calculate loss and gradients.
loss = regularizedLoss(Xrecon,X);
[gradientsE,gradientsD] = dlgradient(loss,netE.Learnables,netD.Learnables);
end
function loss = regularizedLoss(Xrecon,X)
% Image Reconstruction loss.
reconstructionLoss = l2loss(Xrecon, X, ‘NormalizationFactor’,’all-elements’);
% Combined loss.
loss = reconstructionLoss;
end
function Xrecon = modelPredictions(netE,netD,mbq)
Xrecon = [];
shuffle(mbq)
% Loop over mini-batches.
while hasdata(mbq)
X = next(mbq);
% Pass through encoder
Z = predict(netE,X);
% Pass through decoder to get reconstructed images
XGenerated = predict(netD,Z);
% Extract and concatenate predictions.
Xrecon = cat(4,Xrecon,extractdata(XGenerated));
end
end
function X = preprocessMiniBatch(Xcell)
% Concatenate.
X = cat(4,Xcell{:});
endI am building a convolutional autoencoder using a custom training loop. When I attempt to reconstruct the images, the network’s output degenerates to guessing the same incorrect value for all inputs. However, training the autoencoder in a single stack with the trainnet function works fine, indicating that the gradient updates are unable to bridge the bottleneck layer in the custom training loop. Unfortunately, I need to use the custom training loop for a different task and am prohibited from using TensorFlow or PyTorch.
What is the syntax to ensure that the encoder is able to update based on the decoder’s reconstruction performance?
%% Functional ‘trainnet’ loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
aeLayers = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
autoencoder = dlnetwork(aeLayers);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
options = trainingOptions("adam", …
InitialLearnRate=learnRate,…
MaxEpochs=30, …
Plots="training-progress", …
TargetDataFormats="SSCB", …
InputDataFormats="SSCB", …
MiniBatchSize=miniBatchSize, …
OutputNetwork="last-iteration", …
Shuffle="every-epoch");
autoencoder = trainnet(dlarray(xTrain, ‘SSCB’),dlarray(xTrain, ‘SSCB’), …
autoencoder, ‘mse’, options);
%% Testing
YTest = predict(autoencoder, dlarray(xTest, ‘SSCB’));
indices = randi(size(xTest, 4), [1, size(xTest, 4)]); % Shuffle YTest & xTest
xTest = xTest(:,:,:,indices); YTest = YTest(:,:,:,indices);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = extractdata(YTest(:,:,:,1:numImages));
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Nonfunctional Custom Training Loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Encoder
layersE = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer];
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
layersD = [
imageInputLayer(projectionSize)
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
netE = dlnetwork(layersE);
netD = dlnetwork(layersD);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
% Create training minibatchqueue
dsTrain = arrayDatastore(xTrain,IterationDimension=4);
numOutputs = 1;
mbq = minibatchqueue(dsTrain,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFormat="SSCB", …
MiniBatchFcn=@preprocessMiniBatch,…
PartialMiniBatch="return");
%Initialize the parameters for the Adam solver.
trailingAvgE = [];
trailingAvgSqE = [];
trailingAvgD = [];
trailingAvgSqD = [];
%Calculate the total number of iterations for the training progress monitor
numIterationsPerEpoch = ceil(size(xTrain, 4) / miniBatchSize);
numIterations = numEpochs * numIterationsPerEpoch;
epoch = 0;
iteration = 0;
%Initialize the training progress monitor.
monitor = trainingProgressMonitor( …
Metrics="TrainingLoss", …
Info=["Epoch", "LearningRate"], …
XLabel="Iteration");
%% Training
while epoch < numEpochs && ~monitor.Stop
epoch = epoch + 1;
% Shuffle data.
shuffle(mbq);
% Loop over mini-batches.
while hasdata(mbq) && ~monitor.Stop
% Assess validation criterion
iteration = iteration + 1;
% Read mini-batch of data.
X = next(mbq);
% Evaluate loss and gradients.
[loss,gradientsE,gradientsD] = dlfeval(@modelLoss,netE,netD,X);
% Update learnable parameters.
[netE,trailingAvgE,trailingAvgSqE] = adamupdate(netE, …
gradientsE,trailingAvgE,trailingAvgSqE,iteration,learnRate);
[netD, trailingAvgD, trailingAvgSqD] = adamupdate(netD, …
gradientsD,trailingAvgD,trailingAvgSqD,iteration,learnRate);
updateInfo(monitor, …
LearningRate=learnRate, …
Epoch=string(epoch) + " of " + string(numEpochs));
recordMetrics(monitor,iteration, …
TrainingLoss=loss);
monitor.Progress = 100*iteration/numIterations;
end
end
%% Testing
dsTest = arrayDatastore(xTest,IterationDimension=4);
numOutputs = 1;
ntest = size(xTest, 4);
indices = randi(ntest,[1,ntest]);
xTest = xTest(:,:,:,indices);% Shuffle test data
mbqTest = minibatchqueue(dsTest,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFcn=@preprocessMiniBatch, …
MiniBatchFormat="SSCB");
YTest = modelPredictions(netE,netD,mbqTest);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = YTest(:,:,:,1:numImages);
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Functions
function [loss,gradientsE,gradientsD] = modelLoss(netE,netD,X)
% Forward through encoder.
Z = forward(netE,X);
% Forward through decoder.
Xrecon = forward(netD,Z);
% Calculate loss and gradients.
loss = regularizedLoss(Xrecon,X);
[gradientsE,gradientsD] = dlgradient(loss,netE.Learnables,netD.Learnables);
end
function loss = regularizedLoss(Xrecon,X)
% Image Reconstruction loss.
reconstructionLoss = l2loss(Xrecon, X, ‘NormalizationFactor’,’all-elements’);
% Combined loss.
loss = reconstructionLoss;
end
function Xrecon = modelPredictions(netE,netD,mbq)
Xrecon = [];
shuffle(mbq)
% Loop over mini-batches.
while hasdata(mbq)
X = next(mbq);
% Pass through encoder
Z = predict(netE,X);
% Pass through decoder to get reconstructed images
XGenerated = predict(netD,Z);
% Extract and concatenate predictions.
Xrecon = cat(4,Xrecon,extractdata(XGenerated));
end
end
function X = preprocessMiniBatch(Xcell)
% Concatenate.
X = cat(4,Xcell{:});
end I am building a convolutional autoencoder using a custom training loop. When I attempt to reconstruct the images, the network’s output degenerates to guessing the same incorrect value for all inputs. However, training the autoencoder in a single stack with the trainnet function works fine, indicating that the gradient updates are unable to bridge the bottleneck layer in the custom training loop. Unfortunately, I need to use the custom training loop for a different task and am prohibited from using TensorFlow or PyTorch.
What is the syntax to ensure that the encoder is able to update based on the decoder’s reconstruction performance?
%% Functional ‘trainnet’ loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
aeLayers = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
autoencoder = dlnetwork(aeLayers);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
options = trainingOptions("adam", …
InitialLearnRate=learnRate,…
MaxEpochs=30, …
Plots="training-progress", …
TargetDataFormats="SSCB", …
InputDataFormats="SSCB", …
MiniBatchSize=miniBatchSize, …
OutputNetwork="last-iteration", …
Shuffle="every-epoch");
autoencoder = trainnet(dlarray(xTrain, ‘SSCB’),dlarray(xTrain, ‘SSCB’), …
autoencoder, ‘mse’, options);
%% Testing
YTest = predict(autoencoder, dlarray(xTest, ‘SSCB’));
indices = randi(size(xTest, 4), [1, size(xTest, 4)]); % Shuffle YTest & xTest
xTest = xTest(:,:,:,indices); YTest = YTest(:,:,:,indices);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = extractdata(YTest(:,:,:,1:numImages));
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Nonfunctional Custom Training Loop
clear
close all
clc
% Get handwritten digit data
xTrain = digitTrain4DArrayData;
xTest = digitTest4DArrayData;
% Check that all pixel values are min-max scaled
assert(max(xTrain(:)) == 1); assert(min(xTrain(:)) == 0);
assert(max(xTest(:)) == 1); assert(min(xTest(:)) == 0);
imageSize = [28 28 1];
%% Layer definitions
% Encoder
layersE = [
imageInputLayer(imageSize)
convolution2dLayer(3,32,Padding="same",Stride=2)
reluLayer
convolution2dLayer(3,64,Padding="same",Stride=2)
reluLayer];
% Latent projection
projectionSize = [7 7 64];
numInputChannels = imageSize(3);
% Decoder
layersD = [
imageInputLayer(projectionSize)
transposedConv2dLayer(3,64,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,32,Cropping="same",Stride=2)
reluLayer
transposedConv2dLayer(3,numInputChannels,Cropping="same")
sigmoidLayer(Name=’Output’)
];
netE = dlnetwork(layersE);
netD = dlnetwork(layersD);
%% Training Parameters
numEpochs = 150;
miniBatchSize = 25;
learnRate = 1e-3;
% Create training minibatchqueue
dsTrain = arrayDatastore(xTrain,IterationDimension=4);
numOutputs = 1;
mbq = minibatchqueue(dsTrain,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFormat="SSCB", …
MiniBatchFcn=@preprocessMiniBatch,…
PartialMiniBatch="return");
%Initialize the parameters for the Adam solver.
trailingAvgE = [];
trailingAvgSqE = [];
trailingAvgD = [];
trailingAvgSqD = [];
%Calculate the total number of iterations for the training progress monitor
numIterationsPerEpoch = ceil(size(xTrain, 4) / miniBatchSize);
numIterations = numEpochs * numIterationsPerEpoch;
epoch = 0;
iteration = 0;
%Initialize the training progress monitor.
monitor = trainingProgressMonitor( …
Metrics="TrainingLoss", …
Info=["Epoch", "LearningRate"], …
XLabel="Iteration");
%% Training
while epoch < numEpochs && ~monitor.Stop
epoch = epoch + 1;
% Shuffle data.
shuffle(mbq);
% Loop over mini-batches.
while hasdata(mbq) && ~monitor.Stop
% Assess validation criterion
iteration = iteration + 1;
% Read mini-batch of data.
X = next(mbq);
% Evaluate loss and gradients.
[loss,gradientsE,gradientsD] = dlfeval(@modelLoss,netE,netD,X);
% Update learnable parameters.
[netE,trailingAvgE,trailingAvgSqE] = adamupdate(netE, …
gradientsE,trailingAvgE,trailingAvgSqE,iteration,learnRate);
[netD, trailingAvgD, trailingAvgSqD] = adamupdate(netD, …
gradientsD,trailingAvgD,trailingAvgSqD,iteration,learnRate);
updateInfo(monitor, …
LearningRate=learnRate, …
Epoch=string(epoch) + " of " + string(numEpochs));
recordMetrics(monitor,iteration, …
TrainingLoss=loss);
monitor.Progress = 100*iteration/numIterations;
end
end
%% Testing
dsTest = arrayDatastore(xTest,IterationDimension=4);
numOutputs = 1;
ntest = size(xTest, 4);
indices = randi(ntest,[1,ntest]);
xTest = xTest(:,:,:,indices);% Shuffle test data
mbqTest = minibatchqueue(dsTest,numOutputs, …
MiniBatchSize = miniBatchSize, …
MiniBatchFcn=@preprocessMiniBatch, …
MiniBatchFormat="SSCB");
YTest = modelPredictions(netE,netD,mbqTest);
% Display test images
numImages = 64;
figure
subplot(1,2,1)
preds = YTest(:,:,:,1:numImages);
I = imtile(preds);
imshow(I)
title("Reconstructed Images")
subplot(1,2,2)
orgs = xTest(:,:,:,1:numImages);
I = imtile(orgs);
imshow(I)
title("Original Images")
%% Functions
function [loss,gradientsE,gradientsD] = modelLoss(netE,netD,X)
% Forward through encoder.
Z = forward(netE,X);
% Forward through decoder.
Xrecon = forward(netD,Z);
% Calculate loss and gradients.
loss = regularizedLoss(Xrecon,X);
[gradientsE,gradientsD] = dlgradient(loss,netE.Learnables,netD.Learnables);
end
function loss = regularizedLoss(Xrecon,X)
% Image Reconstruction loss.
reconstructionLoss = l2loss(Xrecon, X, ‘NormalizationFactor’,’all-elements’);
% Combined loss.
loss = reconstructionLoss;
end
function Xrecon = modelPredictions(netE,netD,mbq)
Xrecon = [];
shuffle(mbq)
% Loop over mini-batches.
while hasdata(mbq)
X = next(mbq);
% Pass through encoder
Z = predict(netE,X);
% Pass through decoder to get reconstructed images
XGenerated = predict(netD,Z);
% Extract and concatenate predictions.
Xrecon = cat(4,Xrecon,extractdata(XGenerated));
end
end
function X = preprocessMiniBatch(Xcell)
% Concatenate.
X = cat(4,Xcell{:});
end deep learning, autoencoder, regularization, initialization, custom loops MATLAB Answers — New Questions
Integrating Two Unrelated Values
Hi!
I am trying to integrate two different series that correlate to the same image. I was able to obtain X, Y, and Z values for an image in which the x-values correlate with length, the y-values correlate with width, and the z-values correlate with intensity. However, because these measurements are taken across a rectangular ROI, there are 528 x-values (length) and 504 y-values (width), as the object resembles 1/2 of an ellipse.
I would like the integrate these values so that I can plot (length x width x intensity) for my given shape. I have tried to integrate these values by plotting them on the same scatterplot, however I am not having a lot of sucess. I also am not having any luck finding code that will allow me to integrate these values.
Does anyone know an effective way to integrate two "unrelated" values?Hi!
I am trying to integrate two different series that correlate to the same image. I was able to obtain X, Y, and Z values for an image in which the x-values correlate with length, the y-values correlate with width, and the z-values correlate with intensity. However, because these measurements are taken across a rectangular ROI, there are 528 x-values (length) and 504 y-values (width), as the object resembles 1/2 of an ellipse.
I would like the integrate these values so that I can plot (length x width x intensity) for my given shape. I have tried to integrate these values by plotting them on the same scatterplot, however I am not having a lot of sucess. I also am not having any luck finding code that will allow me to integrate these values.
Does anyone know an effective way to integrate two "unrelated" values? Hi!
I am trying to integrate two different series that correlate to the same image. I was able to obtain X, Y, and Z values for an image in which the x-values correlate with length, the y-values correlate with width, and the z-values correlate with intensity. However, because these measurements are taken across a rectangular ROI, there are 528 x-values (length) and 504 y-values (width), as the object resembles 1/2 of an ellipse.
I would like the integrate these values so that I can plot (length x width x intensity) for my given shape. I have tried to integrate these values by plotting them on the same scatterplot, however I am not having a lot of sucess. I also am not having any luck finding code that will allow me to integrate these values.
Does anyone know an effective way to integrate two "unrelated" values? image analysis, integration, 3d plots, function, matrix manipulation MATLAB Answers — New Questions
Same Name but Not Recognized Variable
Hello everyone, I am having issues when calling upon variables. I have a bunch of variables in this code and when I try to call them or hover over them it doesn’t highlight the others as the same.
I have included some pictures of my issue as I’m not quite sure how to describe it. The first one below shows me attempting to call a function, and the variables in calling that function are shown. However, when I call the function, I get this error: (in red of course)
The problem I’ve noticed is when I highlight a variable within calling that function, in the photo below I’ve highlighted "CurrThrottleData", it isn’t highlighted everywhere else. A few places, but not in the function definition below.
Same issue is shown below with "fileName"
My next thought was maybe the function name is having issues or it’s because I have the code separated into sections, but the function name lights up just fine in a different section. It previously worked when the function was called from a separate matlab file but it is really important that the function definitions and the code runs in one parent file for it all.
Any advice and help is appreciate, I have tried the following:
-Control f and replace everything with the same name but different capitalization to make sure it’d change it everywhere, this had no effect.
-Changing the name entirely to something different and manuall copy and pasting the name
-Copying the code in segments and running it in a new file
-Closing and opening matlab incase it needed a brain break… i guess hahaHello everyone, I am having issues when calling upon variables. I have a bunch of variables in this code and when I try to call them or hover over them it doesn’t highlight the others as the same.
I have included some pictures of my issue as I’m not quite sure how to describe it. The first one below shows me attempting to call a function, and the variables in calling that function are shown. However, when I call the function, I get this error: (in red of course)
The problem I’ve noticed is when I highlight a variable within calling that function, in the photo below I’ve highlighted "CurrThrottleData", it isn’t highlighted everywhere else. A few places, but not in the function definition below.
Same issue is shown below with "fileName"
My next thought was maybe the function name is having issues or it’s because I have the code separated into sections, but the function name lights up just fine in a different section. It previously worked when the function was called from a separate matlab file but it is really important that the function definitions and the code runs in one parent file for it all.
Any advice and help is appreciate, I have tried the following:
-Control f and replace everything with the same name but different capitalization to make sure it’d change it everywhere, this had no effect.
-Changing the name entirely to something different and manuall copy and pasting the name
-Copying the code in segments and running it in a new file
-Closing and opening matlab incase it needed a brain break… i guess haha Hello everyone, I am having issues when calling upon variables. I have a bunch of variables in this code and when I try to call them or hover over them it doesn’t highlight the others as the same.
I have included some pictures of my issue as I’m not quite sure how to describe it. The first one below shows me attempting to call a function, and the variables in calling that function are shown. However, when I call the function, I get this error: (in red of course)
The problem I’ve noticed is when I highlight a variable within calling that function, in the photo below I’ve highlighted "CurrThrottleData", it isn’t highlighted everywhere else. A few places, but not in the function definition below.
Same issue is shown below with "fileName"
My next thought was maybe the function name is having issues or it’s because I have the code separated into sections, but the function name lights up just fine in a different section. It previously worked when the function was called from a separate matlab file but it is really important that the function definitions and the code runs in one parent file for it all.
Any advice and help is appreciate, I have tried the following:
-Control f and replace everything with the same name but different capitalization to make sure it’d change it everywhere, this had no effect.
-Changing the name entirely to something different and manuall copy and pasting the name
-Copying the code in segments and running it in a new file
-Closing and opening matlab incase it needed a brain break… i guess haha codeprocessing, matlab, variable-errors, data, function, functions, matlab function, embedded matlab function MATLAB Answers — New Questions
I encountered error though the input data to INS block is exactly 3-column
I’m using the uavPackageDelivery example in UAV toolbox and making small modification in this module.(in "fly full mission mode")
i want to integrate an INS sensor into it,and i’m passing necessary parameters in this sensor.
but once the simulation starts,error message is shown below.
I’m passing exactly 3-column array into it,but still received error above.
Am i right to use INS here?if wrong,where else should i use the INS block?
thxI’m using the uavPackageDelivery example in UAV toolbox and making small modification in this module.(in "fly full mission mode")
i want to integrate an INS sensor into it,and i’m passing necessary parameters in this sensor.
but once the simulation starts,error message is shown below.
I’m passing exactly 3-column array into it,but still received error above.
Am i right to use INS here?if wrong,where else should i use the INS block?
thx I’m using the uavPackageDelivery example in UAV toolbox and making small modification in this module.(in "fly full mission mode")
i want to integrate an INS sensor into it,and i’m passing necessary parameters in this sensor.
but once the simulation starts,error message is shown below.
I’m passing exactly 3-column array into it,but still received error above.
Am i right to use INS here?if wrong,where else should i use the INS block?
thx simulink, simulation, sensor, uav toolbox, ins MATLAB Answers — New Questions
Iterate over struct with length>1, with multiple fields
I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow?I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow? I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow? struct, indexing MATLAB Answers — New Questions
simplify and reorganize symbolic expression
Hello everyone,
I am trying to simplify a symbolic expression (which is the result of the script below) but i am having trouble as it is pretty big and there are many parameters. My objective is to reorganize the expression "B" as powers of variables "c1" and "c2" with all the other parameters included in the coefficents of the polynomial expression (they are gonna be substitued by values in other calculations). I have tried with "simplify" and triyng to nest "collect" into one another.
Does anybody have any tips on how to make the process more efficient? Are there any functions, which I am not aware of right now, that can simplify polynomial expression with regards to the coefficients that multiply the different powers? how about with multivariable expressioon as is my case?
also, is there a way to make math expressions with powers, parentesis, fractions, etc. more readable in the command window? now i have the standard formatting setting of matlab which makes long expressions pretty hard to read
Thank you very much
clear; clc;
syms m J a1 a2
syms k1 k2 k_a12 k_a21
syms c1 c2 c_a12 c_a21
syms lambda
% matrices definitions
M = [m 0;
0 J];
K = [k1 + k2 + (k_a12+k_a21) k1*a1 – k2*a2 + (k_a21*a1 – k_a12*a2);
k1*a1 – k2*a2 + (k_a12*a1-k_a21*a2) k1*a1^2 + k2*a2^2 – (k_a12+k_a21)*a1*a2];
C = [c1 + c2 + (c_a12+c_a21) c1*a1 – c2*a2 + (c_a21*a1 – c_a12*a2);
c1*a1 – c2*a2 + (c_a12*a1-c_a21*a2) c1*a1^2 + c2*a2^2 – (c_a12+c_a21)*a1*a2];
det_M = det(M);
det_K = simplify(collect(collect(collect(collect(det(K), 2*a1), a2), a1^2), a2^2));
det_C = simplify(collect(collect(collect(collect(det(C), 2*a1), a2), a1^2), a2^2));
Mat = (lambda^2)*M + lambda*C + K;
determinant = collect(det(Mat), lambda);
coeff = coeffs(determinant, lambda);
b0 = simplify(collect(collect(collect(collect(coeff(1), k_a12), k_a21), k2), k1))
b1 = simplify(collect(collect(collect(collect(coeff(2), 2*a1), a2), a1^2), a2^2))
b2 = simplify(collect(collect(collect(collect(collect(collect(coeff(3), m), J), c1), c2), c_a12), c_a21))
b3 = simplify(collect(collect(collect(collect(collect(collect(coeff(4), a1^2), a2^2), a1), a2), m), J))
B = b1*b2*b3 – b1^2 – b0*b3^2;
B = expand(B);
B = simplify(collect(B, c1))
this is B that i get:
(a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)*(m*(a1^2*k1 + a2^2*k2 – a1*a2*k_a12 – a1*a2*k_a21) + J*(k1 + k2 + k_a12 + k_a21) + c1*c2*(a1 + a2)^2 – c_a12*c_a21*(a1 + a2)^2) – (a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))^2*(k1*k2 – k_a12*k_a21) – (a1 + a2)^4*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)^2Hello everyone,
I am trying to simplify a symbolic expression (which is the result of the script below) but i am having trouble as it is pretty big and there are many parameters. My objective is to reorganize the expression "B" as powers of variables "c1" and "c2" with all the other parameters included in the coefficents of the polynomial expression (they are gonna be substitued by values in other calculations). I have tried with "simplify" and triyng to nest "collect" into one another.
Does anybody have any tips on how to make the process more efficient? Are there any functions, which I am not aware of right now, that can simplify polynomial expression with regards to the coefficients that multiply the different powers? how about with multivariable expressioon as is my case?
also, is there a way to make math expressions with powers, parentesis, fractions, etc. more readable in the command window? now i have the standard formatting setting of matlab which makes long expressions pretty hard to read
Thank you very much
clear; clc;
syms m J a1 a2
syms k1 k2 k_a12 k_a21
syms c1 c2 c_a12 c_a21
syms lambda
% matrices definitions
M = [m 0;
0 J];
K = [k1 + k2 + (k_a12+k_a21) k1*a1 – k2*a2 + (k_a21*a1 – k_a12*a2);
k1*a1 – k2*a2 + (k_a12*a1-k_a21*a2) k1*a1^2 + k2*a2^2 – (k_a12+k_a21)*a1*a2];
C = [c1 + c2 + (c_a12+c_a21) c1*a1 – c2*a2 + (c_a21*a1 – c_a12*a2);
c1*a1 – c2*a2 + (c_a12*a1-c_a21*a2) c1*a1^2 + c2*a2^2 – (c_a12+c_a21)*a1*a2];
det_M = det(M);
det_K = simplify(collect(collect(collect(collect(det(K), 2*a1), a2), a1^2), a2^2));
det_C = simplify(collect(collect(collect(collect(det(C), 2*a1), a2), a1^2), a2^2));
Mat = (lambda^2)*M + lambda*C + K;
determinant = collect(det(Mat), lambda);
coeff = coeffs(determinant, lambda);
b0 = simplify(collect(collect(collect(collect(coeff(1), k_a12), k_a21), k2), k1))
b1 = simplify(collect(collect(collect(collect(coeff(2), 2*a1), a2), a1^2), a2^2))
b2 = simplify(collect(collect(collect(collect(collect(collect(coeff(3), m), J), c1), c2), c_a12), c_a21))
b3 = simplify(collect(collect(collect(collect(collect(collect(coeff(4), a1^2), a2^2), a1), a2), m), J))
B = b1*b2*b3 – b1^2 – b0*b3^2;
B = expand(B);
B = simplify(collect(B, c1))
this is B that i get:
(a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)*(m*(a1^2*k1 + a2^2*k2 – a1*a2*k_a12 – a1*a2*k_a21) + J*(k1 + k2 + k_a12 + k_a21) + c1*c2*(a1 + a2)^2 – c_a12*c_a21*(a1 + a2)^2) – (a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))^2*(k1*k2 – k_a12*k_a21) – (a1 + a2)^4*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)^2 Hello everyone,
I am trying to simplify a symbolic expression (which is the result of the script below) but i am having trouble as it is pretty big and there are many parameters. My objective is to reorganize the expression "B" as powers of variables "c1" and "c2" with all the other parameters included in the coefficents of the polynomial expression (they are gonna be substitued by values in other calculations). I have tried with "simplify" and triyng to nest "collect" into one another.
Does anybody have any tips on how to make the process more efficient? Are there any functions, which I am not aware of right now, that can simplify polynomial expression with regards to the coefficients that multiply the different powers? how about with multivariable expressioon as is my case?
also, is there a way to make math expressions with powers, parentesis, fractions, etc. more readable in the command window? now i have the standard formatting setting of matlab which makes long expressions pretty hard to read
Thank you very much
clear; clc;
syms m J a1 a2
syms k1 k2 k_a12 k_a21
syms c1 c2 c_a12 c_a21
syms lambda
% matrices definitions
M = [m 0;
0 J];
K = [k1 + k2 + (k_a12+k_a21) k1*a1 – k2*a2 + (k_a21*a1 – k_a12*a2);
k1*a1 – k2*a2 + (k_a12*a1-k_a21*a2) k1*a1^2 + k2*a2^2 – (k_a12+k_a21)*a1*a2];
C = [c1 + c2 + (c_a12+c_a21) c1*a1 – c2*a2 + (c_a21*a1 – c_a12*a2);
c1*a1 – c2*a2 + (c_a12*a1-c_a21*a2) c1*a1^2 + c2*a2^2 – (c_a12+c_a21)*a1*a2];
det_M = det(M);
det_K = simplify(collect(collect(collect(collect(det(K), 2*a1), a2), a1^2), a2^2));
det_C = simplify(collect(collect(collect(collect(det(C), 2*a1), a2), a1^2), a2^2));
Mat = (lambda^2)*M + lambda*C + K;
determinant = collect(det(Mat), lambda);
coeff = coeffs(determinant, lambda);
b0 = simplify(collect(collect(collect(collect(coeff(1), k_a12), k_a21), k2), k1))
b1 = simplify(collect(collect(collect(collect(coeff(2), 2*a1), a2), a1^2), a2^2))
b2 = simplify(collect(collect(collect(collect(collect(collect(coeff(3), m), J), c1), c2), c_a12), c_a21))
b3 = simplify(collect(collect(collect(collect(collect(collect(coeff(4), a1^2), a2^2), a1), a2), m), J))
B = b1*b2*b3 – b1^2 – b0*b3^2;
B = expand(B);
B = simplify(collect(B, c1))
this is B that i get:
(a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)*(m*(a1^2*k1 + a2^2*k2 – a1*a2*k_a12 – a1*a2*k_a21) + J*(k1 + k2 + k_a12 + k_a21) + c1*c2*(a1 + a2)^2 – c_a12*c_a21*(a1 + a2)^2) – (a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 – c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))^2*(k1*k2 – k_a12*k_a21) – (a1 + a2)^4*(c1*k2 + c2*k1 – c_a12*k_a21 – c_a21*k_a12)^2 symbolic math, simplify, coefficients, multivariable expression, polynomial, collect, factor MATLAB Answers — New Questions
Help solving linear ordinary differential equation that involves matrix
I need to find the general solution to a linear ordinary differential equation that involves a matrix. The problem is in the form x'(t) = [ matrix] x(t). When I try to solve, it gives me a solution for x and y but neither one is correct. It needs to be in the form c1(e^t)[matrix] + c2(e^t)[matrix]. thank you.I need to find the general solution to a linear ordinary differential equation that involves a matrix. The problem is in the form x'(t) = [ matrix] x(t). When I try to solve, it gives me a solution for x and y but neither one is correct. It needs to be in the form c1(e^t)[matrix] + c2(e^t)[matrix]. thank you. I need to find the general solution to a linear ordinary differential equation that involves a matrix. The problem is in the form x'(t) = [ matrix] x(t). When I try to solve, it gives me a solution for x and y but neither one is correct. It needs to be in the form c1(e^t)[matrix] + c2(e^t)[matrix]. thank you. dsolve, ode MATLAB Answers — New Questions
Matlab GA evaluating incorrect fitness values.
Dear All, I am trying to minimize a function using GA but it "sees" wrong fitness values. My GA script looks like the following:
ObjectiveFunction = @FF;
ConstraintFunction = @constraints;
nvars = 5;
LB = [1e9 1e9 0.1 -1 1e9];
UB = [1.3825E+12 6.45E+11 0.272 +1 1.12E+11];
options = optimoptions(‘ga’,’PlotFcn’,{@gaplotbestf, @gaplotscores},’Display’,’Iter’,’FitnessLimit’,0.25,’PopulationSize’,20);
[HOM_MOD,FF_val] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,options);
the first and last lines of FF are as follows:
function y = FF(moduli_short)
…
E = [3.73E+10 3.62E+10 3.81E+10 3.36E+09 1.83E+10];
y = abs(energies(1)-E(1))/E(1) + abs(energies(2)-E(2))/E(2) + abs(energies(3)-E(3))/E(3) + abs(energies(4)-E(4))/E(4) + abs(energies(5)-E(5))/E(5);
When the first generation is evaluated the plots are displayed but the values don’t make any sense. The fitness values in the plots are on the order of -10^19, while as you can see from FF fitness can’t even be negative. I print fitness values from the FF function just to be sure and they are always somewhere between 5 and 1000, not -10^19. As a result of this abnormality the code never converges (and keeps evaluating the FF function at useless points). Last but not least, the plots don’t progress past 1st generation, and the outputs look like this:
Best Max Stall
Generation Func-count f(x) Constraint Generations
without anything following it.
I tried a simpler code with only 2 inputs and it works fine in this case. Does anyone have any guesses as to why I may be facing this problem?Dear All, I am trying to minimize a function using GA but it "sees" wrong fitness values. My GA script looks like the following:
ObjectiveFunction = @FF;
ConstraintFunction = @constraints;
nvars = 5;
LB = [1e9 1e9 0.1 -1 1e9];
UB = [1.3825E+12 6.45E+11 0.272 +1 1.12E+11];
options = optimoptions(‘ga’,’PlotFcn’,{@gaplotbestf, @gaplotscores},’Display’,’Iter’,’FitnessLimit’,0.25,’PopulationSize’,20);
[HOM_MOD,FF_val] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,options);
the first and last lines of FF are as follows:
function y = FF(moduli_short)
…
E = [3.73E+10 3.62E+10 3.81E+10 3.36E+09 1.83E+10];
y = abs(energies(1)-E(1))/E(1) + abs(energies(2)-E(2))/E(2) + abs(energies(3)-E(3))/E(3) + abs(energies(4)-E(4))/E(4) + abs(energies(5)-E(5))/E(5);
When the first generation is evaluated the plots are displayed but the values don’t make any sense. The fitness values in the plots are on the order of -10^19, while as you can see from FF fitness can’t even be negative. I print fitness values from the FF function just to be sure and they are always somewhere between 5 and 1000, not -10^19. As a result of this abnormality the code never converges (and keeps evaluating the FF function at useless points). Last but not least, the plots don’t progress past 1st generation, and the outputs look like this:
Best Max Stall
Generation Func-count f(x) Constraint Generations
without anything following it.
I tried a simpler code with only 2 inputs and it works fine in this case. Does anyone have any guesses as to why I may be facing this problem? Dear All, I am trying to minimize a function using GA but it "sees" wrong fitness values. My GA script looks like the following:
ObjectiveFunction = @FF;
ConstraintFunction = @constraints;
nvars = 5;
LB = [1e9 1e9 0.1 -1 1e9];
UB = [1.3825E+12 6.45E+11 0.272 +1 1.12E+11];
options = optimoptions(‘ga’,’PlotFcn’,{@gaplotbestf, @gaplotscores},’Display’,’Iter’,’FitnessLimit’,0.25,’PopulationSize’,20);
[HOM_MOD,FF_val] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,options);
the first and last lines of FF are as follows:
function y = FF(moduli_short)
…
E = [3.73E+10 3.62E+10 3.81E+10 3.36E+09 1.83E+10];
y = abs(energies(1)-E(1))/E(1) + abs(energies(2)-E(2))/E(2) + abs(energies(3)-E(3))/E(3) + abs(energies(4)-E(4))/E(4) + abs(energies(5)-E(5))/E(5);
When the first generation is evaluated the plots are displayed but the values don’t make any sense. The fitness values in the plots are on the order of -10^19, while as you can see from FF fitness can’t even be negative. I print fitness values from the FF function just to be sure and they are always somewhere between 5 and 1000, not -10^19. As a result of this abnormality the code never converges (and keeps evaluating the FF function at useless points). Last but not least, the plots don’t progress past 1st generation, and the outputs look like this:
Best Max Stall
Generation Func-count f(x) Constraint Generations
without anything following it.
I tried a simpler code with only 2 inputs and it works fine in this case. Does anyone have any guesses as to why I may be facing this problem? genetic algorithm, plot MATLAB Answers — New Questions
Initialising a Simulink model workspace from MATLAB file – how to determine the parent model?
Hi everybody,
I’ve recently learnt how to make use of the Simulink model worksapce, to keep large numbers of parameters separate from the base workspace. Specifically, I am using a "MATLAB file" (Initialise.m) as the data source, which loads a lot of data used only by this model. However, I find myself interacting with this model in 2 different ways:
When developing the model it is convenient to simply run Initialise.m from the command window, and open the model manually, allowing me to click the run button and interact immediately with the outputs. For this reason the model is always saved with the DataSource set to "model file", so it does not take ages to open.
When using the model in anger it is run within a parfor loop using the sim command, allowing parameter sweeps to be performed. Within this parfor, the data source therefore gets set to Initialise.m, before reinitialising the model using the code below. Finally the model is closed without saving, so it can always be opened quickly, allowing it to be used in way above.
mdlWks.DataSource = ‘MATLAB File’;
mdlWks.FileName = ‘Initialise.m’;
mdlWks.reload
MY QUESTION IS within Initialise.m, is it possible to determine which of the 2 methods is being used? i.e. when that function is being run as part of a model initialisation? This will allow me to set some certain parameters in the correct way, either to some default values (for method 1), or based on the parameter sweeps (for method 2).
Many thanks for any help!
LeeHi everybody,
I’ve recently learnt how to make use of the Simulink model worksapce, to keep large numbers of parameters separate from the base workspace. Specifically, I am using a "MATLAB file" (Initialise.m) as the data source, which loads a lot of data used only by this model. However, I find myself interacting with this model in 2 different ways:
When developing the model it is convenient to simply run Initialise.m from the command window, and open the model manually, allowing me to click the run button and interact immediately with the outputs. For this reason the model is always saved with the DataSource set to "model file", so it does not take ages to open.
When using the model in anger it is run within a parfor loop using the sim command, allowing parameter sweeps to be performed. Within this parfor, the data source therefore gets set to Initialise.m, before reinitialising the model using the code below. Finally the model is closed without saving, so it can always be opened quickly, allowing it to be used in way above.
mdlWks.DataSource = ‘MATLAB File’;
mdlWks.FileName = ‘Initialise.m’;
mdlWks.reload
MY QUESTION IS within Initialise.m, is it possible to determine which of the 2 methods is being used? i.e. when that function is being run as part of a model initialisation? This will allow me to set some certain parameters in the correct way, either to some default values (for method 1), or based on the parameter sweeps (for method 2).
Many thanks for any help!
Lee Hi everybody,
I’ve recently learnt how to make use of the Simulink model worksapce, to keep large numbers of parameters separate from the base workspace. Specifically, I am using a "MATLAB file" (Initialise.m) as the data source, which loads a lot of data used only by this model. However, I find myself interacting with this model in 2 different ways:
When developing the model it is convenient to simply run Initialise.m from the command window, and open the model manually, allowing me to click the run button and interact immediately with the outputs. For this reason the model is always saved with the DataSource set to "model file", so it does not take ages to open.
When using the model in anger it is run within a parfor loop using the sim command, allowing parameter sweeps to be performed. Within this parfor, the data source therefore gets set to Initialise.m, before reinitialising the model using the code below. Finally the model is closed without saving, so it can always be opened quickly, allowing it to be used in way above.
mdlWks.DataSource = ‘MATLAB File’;
mdlWks.FileName = ‘Initialise.m’;
mdlWks.reload
MY QUESTION IS within Initialise.m, is it possible to determine which of the 2 methods is being used? i.e. when that function is being run as part of a model initialisation? This will allow me to set some certain parameters in the correct way, either to some default values (for method 1), or based on the parameter sweeps (for method 2).
Many thanks for any help!
Lee simulink, model workspace, reinitialize from source, initialize model MATLAB Answers — New Questions
Arduino Mega interfacing problem
I’m using matlab R2013a,I have installed the arduino package suggested by Matlab Websit,when I connect the arduino and type a=arduino in command,I got error as,
>> a = arduino
Undefined function or variable ‘arduino’.I’m using matlab R2013a,I have installed the arduino package suggested by Matlab Websit,when I connect the arduino and type a=arduino in command,I got error as,
>> a = arduino
Undefined function or variable ‘arduino’. I’m using matlab R2013a,I have installed the arduino package suggested by Matlab Websit,when I connect the arduino and type a=arduino in command,I got error as,
>> a = arduino
Undefined function or variable ‘arduino’. arduino, error MATLAB Answers — New Questions
For and While loops
How do I use Embedded “For” or “While” loops to scan through an image in MATLAB?How do I use Embedded “For” or “While” loops to scan through an image in MATLAB? How do I use Embedded “For” or “While” loops to scan through an image in MATLAB? loops MATLAB Answers — New Questions
Set EOIMode using VisaDev to avoid error from *IDN?
It appears that the Visadev function performs a *IDN? when trying to open the connection to a GPIB instrument. This allows it to populate the the information such as model, etc. The issue is that some instruments do not support *IDN? and of those instruments there are some that need the EOIMode set to off in order to function correctly.
So is there a way to suppress sending the *IDN? when using the visadev or is there a way to specify the identity command it is using? Also, is there a way to set the EOIMode before calling visadev? Because it seems like the only way this works now is that you call visadev to connect to the device, it errors out because it needs EOIMode off first and it did not understand what *IDN? command was. At this point you would have to clear the error on the instrument caused by sending an unknown command each time a connection was made.It appears that the Visadev function performs a *IDN? when trying to open the connection to a GPIB instrument. This allows it to populate the the information such as model, etc. The issue is that some instruments do not support *IDN? and of those instruments there are some that need the EOIMode set to off in order to function correctly.
So is there a way to suppress sending the *IDN? when using the visadev or is there a way to specify the identity command it is using? Also, is there a way to set the EOIMode before calling visadev? Because it seems like the only way this works now is that you call visadev to connect to the device, it errors out because it needs EOIMode off first and it did not understand what *IDN? command was. At this point you would have to clear the error on the instrument caused by sending an unknown command each time a connection was made. It appears that the Visadev function performs a *IDN? when trying to open the connection to a GPIB instrument. This allows it to populate the the information such as model, etc. The issue is that some instruments do not support *IDN? and of those instruments there are some that need the EOIMode set to off in order to function correctly.
So is there a way to suppress sending the *IDN? when using the visadev or is there a way to specify the identity command it is using? Also, is there a way to set the EOIMode before calling visadev? Because it seems like the only way this works now is that you call visadev to connect to the device, it errors out because it needs EOIMode off first and it did not understand what *IDN? command was. At this point you would have to clear the error on the instrument caused by sending an unknown command each time a connection was made. visadev, eoimode, idn?, *idn?, identify, visa, handles MATLAB Answers — New Questions
password field is not taking input
In my ubuntu 22.04 I was installing matlab R2023a , during the process after filling email id the password field is not rasponding( not taking input)In my ubuntu 22.04 I was installing matlab R2023a , during the process after filling email id the password field is not rasponding( not taking input) In my ubuntu 22.04 I was installing matlab R2023a , during the process after filling email id the password field is not rasponding( not taking input) password filed is not taking input MATLAB Answers — New Questions