/r/matlab

Photograph via //r/matlab

Official MATLAB subreddit


MATLAB news, code tips and tricks, questions, and discussion! We are here to help, but won't do your homework or help you pirate software.

The effort you put into asking a question is often matched by the quality of our answers.

r/matlab discord channel


Sort By Topic

    Homework     Technical

    Code Share   News

    Tips             Misc


Places to learn Matlab
Matlab Resources

Try saturnapi to share and run MATLAB code in a web browser!

If you want flair simply Message the mods


/r/matlab

61,246 Subscribers

2

Speed up algorithm and memory issues

Hi everyone,

I am trying to do the following computations but my matrices are very large (over 6.000.000 lines) and as you can imagine it takes ages and at some point I get an out of memory error. More precisely, for each Director in my BEorg_sum_us table I want to find the number of previous Roles that he had from the boardexindemploymentus table.

uqDiDs = unique( BEorg_sum_us.DirectorID );
BEorg_sum_us.NumRoles = NaN( height( BEorg_sum_us ), 1);

tic
for i = 1:100 %numel(uqDiDs)
    inds = BEorg_sum_us.DirectorID == uqDiDs(i);
    tmp = BEorg_sum_us( inds, :);
    tmpEmpl = boardexindemploymentus( ismember(boardexindemploymentus.DirectorID,  uqDiDs(i) ), : );
    numRoles = nan( height(tmp), 1);

    if ~isempty(tmpEmpl)

        for j = 1:height( tmp )
            roles = tmpEmpl( tmpEmpl.StartYear < tmp.AnnualReportDate(j), 'Topics' );
            numRoles(j) = height( unique( roles ) );
        end

        BEorg_sum_us.NumRoles(inds) = numRoles;

    end

end
toc

This approach I estimate that it need about 6 hours.

I have tried to cast everything inside the for loop into a function and then use parfor but I get the out of memory treatment.

uqDiDs = unique( BEorg_sum_us.DirectorID );
BEorg_sum_us.NumRoles = NaN( height( BEorg_sum_us ), 1);
NumRoles = cell( height( uqDiDs ), 1);
tic
for i = 1:100 %numel(uqDiDs)
   NumRoles{i} = functionalRoles(BEorg_sum_us, boardexindemploymentus, uqDiDs(i) );
end

for i = 1:100
    inds = BEorg_sum_us.DirectorID == uqDiDs(i);
    BEorg_sum_us.NumRoles(inds) = NumRoles{i};
end
toc

As a final approach I have tried to use a tall array for boardexindemploymentus whihc is over 6000000 lines but it take about 4-5 minutes for one iteration. In the above example I run it for the first 100 uqDiDs but I have around 140.000.

Any help to reduce computation time and optimise memory usage is much appreciated! Thank you in advance.

0 Comments
2024/10/16
07:15 UTC

1

ButtonDownFcn disappeared

Idk what happened but after deleting some callback functions in app design, the selection of ButtonDownFcn is missing in buttons and only ValueChangedFcn is available. I tried reinstalling but nothing changed. How do I make the ButtonDownFcn back again?

Edit: I also discovered that the ButtonDownFcn is only available in UI figure but not in other components

0 Comments
2024/10/16
01:36 UTC

0

Help coding variables

I need to create a code that rejects anything input thats not a whole number, im a beginner and in need of help asap :)

9 Comments
2024/10/15
13:12 UTC

15

You have a ODE to solve? Try Solve ODE Live Task in R2024b

Choose your ODE type, enter the function and parameters, select an automatic or manual solver, and visualize your results.

Solve ODE Live Task

1 Comment
2024/10/15
11:43 UTC

0

Why not help?

I have open the help site, and I test the code. The code ran out badly(see in picture below).

result not ran out well

Why stop when not train well? What is the matter? You are not helping at all! You are making it more and more confusing!! What is the deal? There is even no one that is checking the code or the example?What is going on with matlab staff?

1 Comment
2024/10/15
01:45 UTC

4

Problems getting data from an image

So I am trying to convert the white image of a graph in which x-axis is engine RPMs and y-axis is torque. The curves are isolines for different engine efficiencies. I have two problems:

  1. I am struggling to get a "1D" version of the curves, as they are somewhat thick and I would prefer them to be single point curves.
  2. I want to assign the elipse-shaped curves to different engine efficiencies to later be able to get the efficiency if RPM and torque are inputs.

The approach I am following is:

  • Read the image file.
  • Convert to gray (rgb2gray).
  • Binarize the gray image (imbinarize).
  • From the binarized array assign values.
    • Then I get the resulting array with the deficiencies I mentioned.

Thanks in advance.

Original image

Matlab output

img = imread('graph1.jpg');

grey_img = rgb2gray(img);

binarized = imbinarize(grey_img);

%thin_curves = bwmorph(binarized, 'thin', inf);
thin_curves = binarized;

xmin = 1100;
xmax = 7500;
ymin = 0;
ymax = 300;

data = zeros(1, 2);
indx = 1;

for i = 1:size(thin_curves, 1)
    for j = 1:size(thin_curves, 2)
        if thin_curves(i, j) == 0
            data(indx, 1) = j * (xmax - xmin) / size(thin_curves, 2) + xmin;
            data(indx, 2) = i * (ymin - ymax) / size(thin_curves, 1) + ymax;
            indx = indx + 1;
        end
    end
end
3 Comments
2024/10/14
21:32 UTC

1

Problem with scaling

So I have a Problem where I need to scale the x-axis by a factor while also setting a specific x-value instead of the one in my data (Excel sheet).

I hope you understand what I am trying to day.

Does anyone know how to do this?

2 Comments
2024/10/14
21:01 UTC

1

Want to learn

Is there any course available to learn MATLAB Simulink for free

4 Comments
2024/10/14
14:38 UTC

1

assembly and disassembly processes in production network

hello guys, i am implementing the network shown on the frist image using simevents in matlab simulink. it consists of five machines and buffers in between them. in the second picture is my current implementation( you ddont have to focus on all of these). my problem is in the assembly nd disassembly processes. the article that describes the network says the following about those processes: the transfer line module includes a machine Mi which takes unfinished items from an upstream buffer Bj,i and after processing, sends them to a downstream buffer Bi,l (Fig. 1(a)). The assembly operation is presented in Fig. 1(b). A machine Mi obtains two or more parts or subassemblies, following an assembly factor δj,i from more than one upstream buffers Bj,i, brings them together to form a single unit, which is sent to a downstream buffer Bi,l. The disassembly operation involves a machine Mi taking unfinished single units from one upstream buffer Bj,i, separates them to two or more parts or subassemblies following a disassembly factor di,j, and sends them to downstream buffers Bi,k, as shown in Fig. 1(c). i cant figure out how to implement those with the current blocks simevents has available. i need to define some fixed assembly and disassembly factors , any ideas?

simevents available blocks

my implementation

networks diagram

.

0 Comments
2024/10/14
10:47 UTC

1

Simulink help

Hey,

i have an exercise that i just cant figure out.

"In aviation, hydraulic systems are used, for example, to control the flight control surfaces, high-lift devices and landing gear of an aircraft. Below is a simplified hydromechanical system that extends and retracts a flap of an aircraft. The control signal of the directional valve is ±10 V and the stroke of the hydraulic cylinder 1.4 m. Open the Simulink template and build an open loop controller that’s connected to the unit delay block leading to the valve actuator. For take-off the flaps are set in the middle position and then retracted afterwards. Tune the controller so that the cylinder performs the same movement as in the figure below as closely as possible."

In the pic there's a cylinder target position graph. From 0-4 secs it stays at 0, from 4 to 6 seconds it goes to 0.7 m and stays at it until 14 seconds. From there it goes back to 0 at 16 seconds.

Picture of the simulink model given.

https://preview.redd.it/zpuglsp27pud1.png?width=1240&format=png&auto=webp&s=0f57b88148ef0df9d882bc3308a3a49d0728268c

1 Comment
2024/10/14
10:20 UTC

0

Matlab Assignment Help Website

Hello. I have a Matlab assignment on telecommunication engineering. I tried to ask it in chegg.com, but unfortunately their codes didn't work at all. Is there any trustworty website that I can ask my assignment?

4 Comments
2024/10/13
21:56 UTC

0

My code will not run

Hi, when I try to run this code, it will not run. I asked my professor about it on Wednesday at the very end of our last class and if I remember correctly, she said it had something to do with HW4, but I can't remember what.

The error is

Unrecognized function or variable 'ptm'.

Error in HW6_loadCTDcchdo (line 56)

raw.ptm = ptm(raw.tem, raw.prs);

Can someone please help me figure this out? I am about to leave for work so I will not be able to respond to any comments until 7:30 if I need to give follow up information. Thank you.

The first four pictures are the current HW and the last one is a picture of the PDF of HW4.

EDIT: Sorry, I dont use my laptop for reddit usually, here are the pictures! Thank you again!

https://preview.redd.it/229khykhdmud1.jpg?width=1194&format=pjpg&auto=webp&s=21fd0c54f55cf0bed94ba231439ddba286b471d6

https://preview.redd.it/ttrydhridmud1.jpg?width=614&format=pjpg&auto=webp&s=4c1ab7175dac811f59d93160c68b110702250f75

https://preview.redd.it/szhdtszjdmud1.jpg?width=865&format=pjpg&auto=webp&s=2efd32f7dfbebd79562467e976d4287f4a5cadc0

https://preview.redd.it/75sjrvlldmud1.jpg?width=620&format=pjpg&auto=webp&s=49a5227addacd0b4ebd03c6c410e3d8a86253b33

https://preview.redd.it/iik0456ndmud1.jpg?width=1035&format=pjpg&auto=webp&s=bdcb31985ab2a78cdc461980d9015229b2cc08a6

https://preview.redd.it/rgaumr3odmud1.jpg?width=967&format=pjpg&auto=webp&s=fecea899a9c7bbeb498f736dd361bcb2c22761ed

6 Comments
2024/10/13
18:28 UTC

1

importing STEP files to matlab

for a university project i am supposed to import a 3D model into matlab, i have all the step files and xml files and code for the matlab assembly i just dont know how to import it. I found information on using smimport but i dont think thats my case. Anyone got any tips?

3 Comments
2024/10/13
17:02 UTC

1

Linking files in matlab code

Hi I am trying to link two files, Ybus.m and Jacobian.m into my matlab code.

This is the code I am trying to run:

clear alladdpath('C:\Users\sabri\OneDrive\Documents\ECE6320');

which Jacobian.m

which Ybus.m

Jacobian; % File from PW, includes the sparse Jac

YBus; % File from PW, includes Ybus and V variables

I defined the path specifically and when I run which it points to the correct file. However when I just try to actually pull up the Jacobian or Ybus values themselves the program doesn't recognize the variables. I've verified that both files have the correct inputs in them. However when I open the Jacobian file in Matlab I get this error

https://preview.redd.it/ktv0b8f0giud1.png?width=698&format=png&auto=webp&s=baabd9a074c2680463fcd0cf2885e15d96a49d74

Does anyone know how to fix this and link properly?

3 Comments
2024/10/13
11:39 UTC

1

Velocity profile of a half-car model on bumpy road

I am trying to inspect the velocity profile of a half-car model on Simulink when subjected to bumpy road conditions. I can somehow figure out the half-car model, but I have no clue how to introduce road bumps as input.

Is it possible to use a real-world dataset of rough terrain as excitation input to the model?

any kind of advice is appreciated.

Sorry for the inconvenience.

1 Comment
2024/10/13
03:38 UTC

354

im sorry

6 Comments
2024/10/12
16:55 UTC

1

ANY CLUE?

Specs:

CPU: i5

GPU:RTX 4060

MATLAB works very slow. takes way long to load and opening other layouts and add ins takes way to long.

4 Comments
2024/10/12
14:18 UTC

5

Matlab Projects

Hi,

I am a second-year student, and I have to do a MATLAB project as homework for my university. I have several ideas, but I haven’t decided yet.

1.	Football Match Bet Analysis
2.	Image Compression
3.	Real Estate Price Analysis
4.	Stock Price Prediction
5.	Portfolio Optimization

Actually, all of them interest me. By improving this project, I can learn data analysis or machine learning skills. What do you think about these projects? Which one would be the most beneficial?

3 Comments
2024/10/12
11:02 UTC

3

Is there a MATLAB app compiler for mac?

I have a standalone application from MATLAB app designer, and was wondering if there is any compiler that can produce installation files for mac systems.

6 Comments
2024/10/11
21:00 UTC

2

Help with question

I am struggling to work out the solution to this tutorial question using matlab. Any help would be greatly appreciated. F1 and F2 are forces with magnitudes 8 N and 10 N, respectively. They act on a particle located at the origin.

The force F1 acts in the positive horizontal direction, the force F2 is inclined at an angle of 30◦ (counter-clockwise) with respect to F1.

Define F1 and F2 in 2D Cartesian components (e.g. F1 = [F1_x, F1_y], F2=[F2_x,F2_y]), assign them to MATLAB variables F1 and F2.

Determine the resultant force R acting on the particle:

the vector of the R in Cartesian form, e.g. R=[R_x,R_y]

the magnitude of the resultant force R, assign it to the MATLAB variable R_abs. DO NOT USE norm() FUNCTION of MATLAB.

the angle (in degrees) of the resultant force R with the positive direction of x-axis, assign to the MATLAB variable theta.

5 Comments
2024/10/11
17:36 UTC

0

Why linspace and [start : step : end] aren't the same?

n = 120; x1 = [0:2pi/120:2pi]; x2 = linspace(0, 2*pi, n+1); if (x1 == x2)
disp("equal") else
disp("no") end Output: no Why don't these methods yield the same output?

5 Comments
2024/10/11
16:33 UTC

0

seeking for a matlab tutor ASAP!!

I’m studying chem eng and we started matlab and i realized it would be hard for me to pass without any external help

27 Comments
2024/10/11
14:36 UTC

3

Matlab Coder conversion to C question

Hi. I am trying to convert my Matlab algorithm to C-code using the Matlab Coder application. One step in the algorithm is using the wavelet transform and reconstructing the signal with Matlab’s “wrcoef” function for a specific detail level. The problem is that this function can’t be used with the Matlab Coder. I have tried other methods for reconstruction like Matlab’s “waverec” function, but the reconstruction output is different than that with the “wrcoef” function. Does anyone have a work around to get the output of the “wrcoef” function and make it compatible with the Matlab Coder application?

2 Comments
2024/10/11
12:45 UTC

3

linearization of State Space

https://preview.redd.it/aojtqfsm44ud1.jpg?width=1920&format=pjpg&auto=webp&s=2c34250325996dffef913330ae1ef9cde8314b15

the figure above is results of my simulink output for angular displacement. but the state space is diverging away. this is the equation [(𝑚1 + 𝑚2 ) ∙ 𝑙1 2 ∙ ddot𝜃1 + (𝑚1 + 𝑚2 ) ∙ 𝑙1 ∙ 𝑔 ∙ sin(𝜃1 ) = T], how can i linearize this in state space representation?

also examiner says that: Note: To create the state space, you will need to linearize the system. Use the condition of small angles, i.e. the sine of a small angle can be considered as the angle itself (𝑠𝑖𝑛 𝜃1 ≈ 𝜃1 𝑖𝑓 𝜃1 𝑖𝑠 𝑠𝑚𝑎𝑙𝑙). i did this but no good result as you can see in the figure

11 Comments
2024/10/11
11:34 UTC

1

Advice for SerDes Design

I wanted to design an 8:1 LVDS SerDes in Matlab using SerDes Designer tool( please suggest if there's any other better method) , what's the flow of the design process and is there any beginner friendly material to understand the flow of the design

2 Comments
2024/10/11
09:15 UTC

Back To Top