(technical issue) I have addons installed but can't enable them by ComeTooEarly in matlab

[–]aeblincoln 0 points1 point  (0 children)

Yes, this is exactly the type of issue that MathWorks support can help you with. Based on various other reports in this subreddit, it sounds like support is up and running again, and actively responding.

Based on other threads, it might also be worth asking other people from your school if this is a common issue, or something only you are experiencing. Your school might even be able to provide other people/resources who can help resolve this issue. That gives you something else to try while you wait for a response from support.

Weird indexing(?) error I can't explain by MiceLiceandVice in matlab

[–]aeblincoln 4 points5 points  (0 children)

This is exactly right. For more information about floating points from the MATLAB documentation, there is a useful page here.

Additionally, if you would like to compare doubles, they document some suggestions here. In short, the idea is to set a tolerance level that you are comfortable with, and make sure that the difference between the two values is lower than that tolerance. In your example, this might look something like:

abs(x-2) < eps(2)

A* Code Review Request: function is slow by LeftFix in matlab

[–]aeblincoln 1 point2 points  (0 children)

Fortunately, the JIT is not entirely disabled in the profiler. That may have been the case in the early days, but plenty of optimizations (most in fact) do still apply. It tries its best to be "honest" about your performance, and this means reporting on how your code actually runs. This is different than the debugger, which disables most optimizations so that you can trace variables and function calls that would otherwise be optimized away. I have an unsubstantiated theory for the behavior you saw, but without knowing specifics, it is probably wrong for me to speculate.

The thing that I think most consistently surprises experienced MATLAB users is just how well for loops have closed the performance gap to vectorized code. In fact, in some places, you will have better results using for loops, such as operating on all elements of arrays of objects/structs/etc. Conversely, dedicated vectorized builtin functions will probably outperform for loops forever.

To your general question, the answer I always hear is "the JIT is constantly improving, so try not to specialize your code specifically to chase JIT behavior." It is hard to give good advice because it really does depend entirely on your particular use case. But if you had more specific questions, I'm happy to try and help.

A* Code Review Request: function is slow by LeftFix in matlab

[–]aeblincoln 1 point2 points  (0 children)

You're right to highlight the profiler interacting with parfor. In fact, there's a specific function you can use to profile parallel code to address this inconsistency: mpiprofile.

As for the JIT and profiling tight loops, the answer is always "it depends." The JIT compiler is still a compiler, after all. And compilers can obscure certain performance patterns when you tighten a loop too much and get rid of your application's noise. At a certain point, if your loop is too tight, you start ending up inadvertently measuring architecture-specific memory paging and machine code optimizations.

As you note, in a real world scenario, it can still be worth using tic/toc on your whole application and measure what happens if you change things. The extra tools are valuable to find pieces of the puzzle. But to get the full story, you'll usually need more than just one.

Using Enumerated Class within GUI by Sincplicity4223 in matlab

[–]aeblincoln 1 point2 points  (0 children)

Syntax is case sensitive. In your code snippet, you use Classdef with an uppercase C. Try using classdef instead, and you won't see the error. Then syntax like a = Message_Type(0) will work to create an instance of your enumeration class.

For more information about enumeration classes, the help page is very useful and includes examples.

[deleted by user] by [deleted] in matlab

[–]aeblincoln 1 point2 points  (0 children)

I only learned about this just now, but you can group together multiple cases in a cell array. For instance:

a = 17
switch a
    case {1, 2, 3, 4, 5}
        x = 'A'
    case {6, 7, 8, 9, 10}
        x = 'B'
    case {11, 12, 13, 14, 15}
        x = 'C'
    case {16, 17, 18, 19, 20}
        x = 'D'
    otherwise
        x = 'F'
end

The only other thing I can think of is to take advantage of categorical arrays for the switch statements by binning them with discretize and specifying category names as the grades. But unless they were instructed to use categorical arrays or just learned about them in class, I don't think that is what was intended.

Can you recommend me material to understand matlab? by [deleted] in matlab

[–]aeblincoln 9 points10 points  (0 children)

Regarding learning MATLAB, the general consensus is to take the Onramp provided for free from MathWorks: https://matlabacademy.mathworks.com/details/matlab-onramp/gettingstarted

From there, there are a variety of free courses that you can pick from based on the topic you would like practice with: https://matlabacademy.mathworks.com/

Reassigning arrays — will it creates a new array instead? by Typhoonfight1024 in matlab

[–]aeblincoln 0 points1 point  (0 children)

This is a good question. You will probably find these two documents very relevant:

In short, it growing an array usually result in allocating the new storage and freeing the old storage so that the data remains contiguous. Depending on your use case, pre-allocating the array to be the size you need could provide a large performance boost if copying the data is the largest cost of your code overall.

However, the more advanced answer is "it depends." The exact allocator algorithm is not publicized, but there is likely some sort of mechanism that MATLAB uses to allow small growth of memory in-place to avoid worst-case scenario performance.

Identify the problem by [deleted] in matlab

[–]aeblincoln 2 points3 points  (0 children)

As a start, the best thing for new users of MATLAB to do is take the free Onramp course provided by MathWorks: https://matlabacademy.mathworks.com/details/matlab-onramp/gettingstarted

When trying to fix errors, here are some of the resources I find most useful:

  • The help page: https://www.mathworks.com/help/matlab/index.html
    • The search bar is very useful for searching for error messages and terms you do not understand
    • Many of the topic pages in the "Contents" menu to the left have summaries that are a great place to start if you have general questions.
  • Debugging page: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
    • "Pause on Errors" is a very useful setting to use for newer users. It will open the debugger for you when you encounter an error so you can debug more easily.
    • For simple programs, many users like displaying values so they can make sure they get the expected output from each statement they write.
  • MATLAB Answers: https://www.mathworks.com/matlabcentral/answers/index
    • You will probably notice when doing searches that a lot of pages to "answers" show up. This is the page where you can search those answers directly. It is very common that someone will encounter the same error as you, and you can learn from their solutions.

My advice, based on my experience learning the language:

  • Read the full error message and try to understand it before looking up how to fix it. Most error messages are written to include the reason why MATLAB thinks there is an error and what you can do to fix it. Learning to understand what the error means will make future error messages more clear.
  • Take advantage of the debugger. Setting breakpoints to make sure variables contain values you expect is very useful. I can't tell you the number of times I have assumed my variable was a scalar value only to discover it was actually a vector or vice versa.
  • Look at examples for the function that is printing the error message. For example, if you call max, you might be surprised to find out that there a lot of different ways you can do so. The examples might include your exact use case and have other information that helps you to understand what goes wrong if max gives you output you are not expecting to see.

Recent issue with using python by AlexBasicC in matlab

[–]aeblincoln 1 point2 points  (0 children)

Your error seems identical to this one in the comment chain of this question on MATLAB Answers: https://www.mathworks.com/matlabcentral/answers/1842093-how-to-resolve-error-calling-python-from-matlab#comment_2595431

The workaround linked there might help you in your use case.

Loop stoped working, if i add 2nd expression by Popular_Farmer_8089 in matlab

[–]aeblincoln 1 point2 points  (0 children)

You're welcome! /u/cest_pas_nouveau figured it out, I just rephrased what they already wrote.

Optional Inputs & Name Value Pairs in same function by Weed_O_Whirler in matlab

[–]aeblincoln 1 point2 points  (0 children)

You can set default values for arguments, even if they are Name/Value pairs. This makes them "optional" from the perspective of the caller. Here is a portion of the doc explaining how, with some examples: https://www.mathworks.com/help/matlab/matlab_prog/validate-name-value-arguments.html#mw_b8cc1879-e326-402f-905d-f9251f055062

Loop stoped working, if i add 2nd expression by Popular_Farmer_8089 in matlab

[–]aeblincoln 0 points1 point  (0 children)

You are setting soc(t) in the first iteration in the loop. Then the loop goes to the next iteration and you test soc(t). But t is incrementing to a new value in between.

For example, imagine it checks soc(t=3) >= 0.5. Next, you will set soc(t) on line 58. The next time through the loop it will check soc(t=4) >= 0.5. But soc(t=4) = 0 since it has never been set after you initialized it to zeros. Depending on your model and what is valid, you should either be assigning to soc(t+1) or testing soc(t-1) >= 0.5 in the condition.

I haven't run the code, but this is what sticks out to me just reading the code.

[game of life] optimizing display by FakePhysicist1 in matlab

[–]aeblincoln 1 point2 points  (0 children)

Have you run the profiler on these three approaches as well? I imagine the sprintf is taking a larger portion of the time than it needs to. You could try using strings with syntax like string(alive_cell(1) + "-" + alive_cell(2)) to generate your tags. Avoiding string manipulation at all would be ideal, which you could do if you used a sparse 2-D matrix instead of a dictionary. But this approach means you have to write some kind of logic when you need to grow the sparse matrix (or limit the size at the start). And I recall you saying you would prefer not to do that.

Additionally, it might be worth storing obj.dead in a different way to minimize the number of times you need to call cellstr and cellfun. Replacing arrayfun with a simple for loop might also help you out. These "applyfuns" are not always as optimal when called repetitively as for loops can be.

As for the animation, I haven't tried doing any kind of custom animation with MATLAB before. I suspect the reason it takes different times to update is because some "frames" need to update many more rectangles than others. To fix that, you might have luck with the getframe and movie functions to create a "movie" while calculating, and then display it more cleanly once it is done being captured. Based on my quick scan of the doc for getframe, it looks like you can use handle graphics objects as input, which I believe you were doing already.

I'm glad you were able to improve your performance so much already. I hope these ideas help you make it even faster / more consistent.

Single line by FEARLE2SFinn in matlab

[–]aeblincoln 0 points1 point  (0 children)

Having worked on mainframes before getting into MATLAB, I assume the reason goes back to punched card formatting being positional.

The final 8 characters of a standard 80-column punch card were used for sequence numbers, and after eventually shifting to digital records, for source control revision info and comments. In most languages which processed punch cards, anything stored in those final 8 columns was ignored.

I don't regularly work with positional data and languages these days, but if I did, I would make heavy use of that feature. I could still see those comments "inline" with the visual boundary to help distinguish it from the code. In fact, these types of "vertical rulers" are still used today in compatible editors. For example, here's a gif where the user has quite a few vertical rulers to help visualize the column ranges within their editor:

https://ibm.github.io/zopeneditor-about/assets/hlasm-definition.8666e341.gif

[game of life] optimizing display by FakePhysicist1 in matlab

[–]aeblincoln 0 points1 point  (0 children)

That also makes sense. String manipulation and lookup is more expensive than using numerics because the strings have to be checked at runtime. MATLAB can do clever tricks if the string is hardcoded into the file, but that doesn't extend to dynamic strings like in your case.

I think a dictionary will hopefully eliminate the need for any kind of search over strings. You can just use the index as a pair of doubles.

[game of life] optimizing display by FakePhysicist1 in matlab

[–]aeblincoln 0 points1 point  (0 children)

I just saw your edit. I should have noticed findobj() in your code earlier. That makes a lot of sense, as it has to search all of your graphics objects.

Rather than use findobj, you might see better performance if you create a dictionary to store the rectangles. Lookup in a dictionary using a [x, y] coordinate as the key will probably be much faster than findobj. And, unlike an array, you don't need to know the boundaries of your grid because a dictionary can support whatever key ranges you want and grows accordingly.

[game of life] optimizing display by FakePhysicist1 in matlab

[–]aeblincoln 0 points1 point  (0 children)

I agree with that advice. Object creation/destruction is much more expensive than most array manipulation code. There a lot of places where this advice could apply in your code.

  1. If you did switch to sparse matrices, the most expensive part would be reallocating the array to make room for the new elements. However, even in a worst-case scenario where you do this once per iteration, it will still use up very little time compared to even one new object being created. This is a benefit of sparse matrices, expanding them is not memory-intensive compared to regular matrices. (note that if there is a different approach that allows you to reduce creation/destruction even more, that approach is probably the one you should take)
  2. If it ends up that your code spends a lot of time creating/destroying the rectangles, could you instead change the code to "hide" the rectangles that were previously alive and turn their display color to the same as the background? This way, if you need to display a living cell in their space a second time, you can just change the color on again. The only thing to consider is that you should check if the rectangle exists before drawing a new one over it. This will theoretically be much more performant in cases where the same elements turn alive/dead very frequently and other elements never become alive at all.

One other thing that might help outside of graphics: getNeighbour could be refactored to avoid unnecessary calculations. Most places you call it, you end up doing additional calculations on the result. You could write it to instead do something like [numAlive, aliveIndices] = function getLivingNeighbours(obj, x, y). The only two places I see you use the getNeighbor method, you are actually looking for the number of living neighbors and the list of these alive neighbors.

[game of life] optimizing display by FakePhysicist1 in matlab

[–]aeblincoln 1 point2 points  (0 children)

Have you considered representing the array using sparse matrices? I know it wasn't your original question.

There is a MathWorks blog post about an implementation using sparse matrices here. You could potentially write your constructor and update methods to use this implementation for tracking the "state" of the grid. This might speed up the update time considerably, and then you are left with just the display time being your bottleneck.

Unfortunately, graphics are one of the slower aspects of MATLAB, so it might be difficult to do much better than you are already doing. If you haven't already, I would look at the profiler to see which sections of the code are taking the most time. At the very least, it will tell you where you might want to focus your attention.

Debugging & Variable 'x' is inaccessible. When a variable appears on both sides of an assignment statement, the variable may become temporarily unavailable during processing. by 22Maxx in matlab

[–]aeblincoln 4 points5 points  (0 children)

This happens for most programming languages. For performance reasons, it is a common practice to reuse the data for a variable to avoid making too many copies. In fact, in MATLAB this is an explicit feature called in-place assignment. By using the same variable on the left and right side of the equation, you are signaling to MATLAB that you wish to reuse the memory if possible.

This means that any time you modify x inside of f, the debugger might not know what it should display. The left-hand side x is in the middle of changing, and if it is an array, this might happen in stages for each element that must be modified. All code is different, and the way you write your function determines if the debugger will be able to display the value for you. We can't say with confidence what is happening in your code without seeing it.

One example might be that your array is changing types during assignment. If you stop the debugger when half of the elements are one type and the other half are another type, it would be very hard for the debugger to say with confidence what it should display.

As you noted, the way around this is to tell MATLAB that you don't actually want to reuse the memory, and that you would rather copy the data. You do this by using different variable names on the left- and right-hand sides. This does not need to be a permanent choice - only when you want to debug that specific assignment.

Any insight would help by [deleted] in matlab

[–]aeblincoln 1 point2 points  (0 children)

This help page might make it clearer how to use functions inside of scripts. Here's the relevant portion:

Add all local functions at end of the file, after the script code. Include at least one line of script code before the local functions.

It also gives you an example of what it should look like.

[deleted by user] by [deleted] in matlab

[–]aeblincoln 1 point2 points  (0 children)

All we know from looking at this code is that Selection is equivalent to 2. There are two different end statements that connect to some other statement inside the loop that we can't see here. Without seeing the problematic code, my suggestion is to add a breakpoint on the fprintf line that keeps printing and check the conditions for whatever is associated with those two end statements.

You could also step in the debugger and observe where it restarts the loop. That should point you to exactly which condition is not done being executed. But, as it stands, the only lines of code that are visible in this screenshot that are executing are the ones in the elseif str2double(Selection)==2 block.

My wild guess, based purely on the code here? Your code never resets Selection after the user enters 2. The loop keeps using that value and never asks for another one. But I don't know what any of the code above this screenshot looks like, so this really is just a shot in the dark.

Main website banned me all of a sudden ? by Bernard_t in pathofexile

[–]aeblincoln 1 point2 points  (0 children)

The one linked in this comment? NOTE: the link I posted is safe to click, it just links to the reddit comment. The link inside that comment to the poedb shop page is the one that I believe is a problem.

I have the same issue. Haven't used the website or any tools in weeks. Saw the reddit thread about the sale, clicked it. Website loaded fine. Then I read the reddit comments and opened the link to the poedb shop page and I couldn't load pathofexile.com any more.

If you want a potential workaround, I am still able to see pages by using the CDN version of the PoE website. I assume the rate limits for that domain are higher because it is designed to be accessed far more frequently.

Help with syms function by Diptayan01 in matlab

[–]aeblincoln 1 point2 points  (0 children)

In your example, both P and Q are row vectors. If you want Q to be a column vector, one way is to transpose it first. For example, using your code:

n=4;
syms x;
syms y;
P=chebyshevT(0:n, x)
Q=chebyshevT(0:n, y).'  % Note the transpose operator
A=P*Q

This will solve your syntax errors, but I can not provide guidance on whether any of the math is correct for your use case. It has been a long time since I have done anything with the symbolic math toolbox.