## Thread: does C++ have a standard function similiar to matlab's fix() ?

January 14th, 2008, 10:52 AM#1

### does C++ have a standard function similiar to matlab's fix() ?

Hello,

Here is the description of matab's fix():

fix(x) : returns integer closest to x in direction towards 0

so fix(-3.4) would return -3 and fix(3.4) would return 3.

Can C++ do this or do I have to write it myself with floor and ceil?

Thanks!### Re: does C++ have a standard function similiar to matlab's fix() ?

You can cast the double to int, upon which truncation would "fix" the value. e.g.,

Code:

std::cout << static_cast<int>(3.4) << ' ' << static_cast<int>(-3.4) << std::endl;

#### Posting Permissions

- You
**may not**post new threads - You
**may not**post replies - You
**may not**post attachments - You
**may not**edit your posts

Forum Rules

## How does pca() function in Matlab fix the sign of principal components? [duplicate]

Given any solution to PCA, a sign-flipped version of it is an equally valid solution. A numerical solver breaks this symmetry by finding one of these equally valid solutions. Implementation details and initial conditions determine which solution the solver will produce.

One way to think about PCA is that it maximizes the sum of the variance of the data projected onto the weight vectors, subject to the constraint that the weight vectors are orthonormal. Say the data set contains $n$ points in a $d$ dimensional space with mean $\mu$. We seek a set of orthonormal vectors $\{v_1, ..., v_p\}$ that solves the following optimization problem:

$$\max_{v_i, ..., v_p} \quad \sum_{i=1}^p \frac{1}{n} \sum_{j=1}^n \left [ (x_j - \mu)^T v_i \right ]^2 \quad \quad s.t. \quad \begin{array}{ll} \|v_i\| = 1 & \forall i \quad \\ v_i^T v_j = 0 & \forall i \ne j \\ \end{array} $$

Say we have a set of weight vectors that solves this problem. You can see that flipping their signs gives the exact same value for the objective function, and the constraints remain satisfied. Hence, the sign-flipped solution is equally valid. There are various other ways of thinking about PCA and writing it as an optimization problem. Some of these ways sound conceptually different, but they all yield the same set of solutions, and the same reasoning holds for all of them.

The reason a particular function implementing PCA returns any one of these solutions over the others comes down to implementation details and initial conditions. Say we're trying to solve the above problem using a standard optimization solver. It starts from some (possibly random) initial set of parameters, then iteratively updates them to increase the value of the objective function, while respecting the constraints. Imagine the objective function as a hilly landscape, where each location corresponds to a particular choice of parameters and the height at each location is the value of the objective function for those parameters. The constraints define particular regions the solver is allowed to go. Each solution to the problem is the highest allowed location on some surrounding hill. There are multiple hills, and the solutions all have the same height (i.e. are equally good). The solver starts from some initial location in this landscape and generally tries to move uphill, eventualy stopping when it can't make any further uphill progress. So, the solution it finally attains is determined by the hill it starts on and how it chooses to step around the landscape.

Of course, one wouldn't typically solve PCA this way because there are more specialized, computationally efficient ways to do it. For example, one popular method is to obtain the weights as eigenvectors of the covariance matrix. But the eigenvalue solver is itself an iterative algorithm, and is subject to the same kinds of issues.

answered Feb 1 '17 at 20:58

28k33 gold badges5858 silver badges9595 bronze badges

## How To Fix Older Versions of MatLab to Run on Windows 7

**First posted to MSDN on Aug, 12 2010**

The fact that I debugged this application already has helped at least one other organization, so rather than forcing everyone to figure this out on their own, I figured I would blog about it.

I worked with a customer who was using MatLab 7.1 R14 SP3. (For the sake of completeness, I’ll also specify that they were running it on Java 1.6.17.) Now, they had the most current version also, which they believed was working on Windows 7. They weren’t trying to avoid an upgrade, and liked the new features. But they were saying that they needed this older version from time to time to process some of their older work. (Apparently data upgrade isn’t as simple? I don’t know the first thing about MatLab – all I know is that I was asked if they could keep it going, and I said I would give it a go.)

When they ran the application, they saw an error message that looked something like this:

This “Error Starting Desktop” message followed by a huge Java stack dump … well, they didn’t find it so helpful.

(Dear fellow developers: please don’t dump stack traces in front of your users.)

What they tried next was a stroke of genius. They put it into Windows 2000 compatibility mode, and it started working. So, when I came in, I had a pretty darn good hint. (This customer was unaware that compatibility modes just use shims, so they were just trying to avoid all of their users from having to manually set this up.) I let them know that they could create a custom shim database to address this. Plus, as long as you’re at it, we could spend a couple of minutes figuring out precisely which shims are actually necessary. We just kept removing shims until it stopped being fixed.

Interestingly enough, the only shim that ended up being actually necessary to fix the application was the Win2000VersionLie shim. That’s right – we didn’t have to actually change anything, we just needed to pretend that we were Windows 2000. (So, apparently, there is some code branch somewhere – either in Java or MatLab – that results in working code being executed on Windows 7 when it thinks it’s running on Windows 2000.)

A little over a month later, I got an email from somebody trying to run MatLab R2006a on Windows 7, and seeing the exact same dialog. I responded (within 2 minutes) “Win2000VersionLie.” so of course he didn’t believe me. So then, I told this person the story and why I thought I knew the answer. Once I did this, he actually had is customer try it.

His response?

“OMG, Chris – This worked!”

**Cluster Fix for Matlab**

**Download Below**

### General Description

Eye tracking is used extensively in research to analyze scan paths in humans and animal subjects. The most common algorithms for analyzing scan paths employ velocity and acceleration thresholds to determine when a saccade occurs since the eye moves faster during a saccade than during a fixation; however, these thresholds are often arbitrary as well as inaccurate. To solve these serious flaws, Cluster Fix uses k-means cluster analysis on distance, velocity, acceleration, and angular velocity. K-means clustering finds natural divisions in data and groups the data in K number of clusters. The number of clusters is determined using a built-in MATLAB function which optimizes the distances between data points within a cluster relative to the separation of clusters in space. Initially, Cluster Fix globally evaluates the whole scan path at once, detects fixations and saccades, and then locally re-evaluates each fixation and saccade pair in order to identify small, short saccades as well as precisely identify the start and end of saccades. Cluster Fix is written in MATLAB code.

Cluster Fix publication provides more detail.

Copyright © 2013-2014, Seth Koenig & Elizabeth Buffalo. All rights reserved.

If utilization of the Cluster Fix algorithm, with or without modification, results in outcomes which will be published, you must site the following publication:

Seth D. König, and Elizabeth A. Buffalo. A nonparametric method for detecting fixations and saccades using cluster analysis: Removing the need for arbitrary thresholds. Journal of Neuroscience Methods, 2014.

Redistribution & and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## Fix matlab

## fix

Main Content

### Description

example

rounds each element of to the nearest integer toward zero. This operation effectively truncates the numbers in to integers by removing the decimal portion of each number:

For positive numbers, the behavior of is the same as .

For negative numbers, the behavior of is the same as .

### Examples

collapse all

### Round Matrix Elements Toward Zero

*3×2*-1.9000 -3.4000 1.6000 2.5000 -4.5000 4.5000

### Round Complex Numbers Toward Zero

*1×3 complex*1.4000 + 2.3000i 3.1000 - 2.2000i -5.3000 +10.9000i

*1×3 complex*1.0000 + 2.0000i 3.0000 - 2.0000i -5.0000 +10.0000i

### Input Arguments

collapse all

### — Input array

scalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array. For complex , treats the real and imaginary parts independently.

converts logical and elements of into values.

**Data Types: ** | | | | | | | | | | | **Complex Number Support: **Yes

### Extended Capabilities

### Tall Arrays

Calculate with arrays that have more rows than fit in memory.

This function fully supports tall arrays. For more information, see Tall Arrays.

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Code generation does not support or data types for .

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

Code generation does not support or data types for .

### Thread-Based Environment

Run code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

### Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

**Introduced before R2006a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

## Fix BrainStorm GUI controls under Matlab 7

I had the same problem seeing the text on the buttons using Matlab 7.2 running on Mac OSX 10.4…however, I was able to see the text by changing bst_color_scheme.m file located in the Toolbox folder.

Go to line 123 and change the variable “textlight” to be [0 0 0]

This will turn the text black and is now visible on the gray buttons.

Hope this helps!

[QUOTE=Birte;465]I just downloaded BrainStorm and started it in Matlab. Everything seems to work fine, just that the control buttons are not labeled. They are not dark colored like in the GUI shown on your website but light (probably just like the text that’s written on them, cause you can see the text appear transiently when pressing the buttons). I already tried the two different color schemes. Is there a fix that you can propose to me? Is it maybe similar to the fix you posted?

I am using Matlab 7.3.0 R2006beta (running via X11) on Mac OS X Tiger. Do you have any experiences on using BrainStorm on Macs or do you know someone doing so?

Cheers,

Birte[/QUOTE]

### Now discussing:

- Minecraft skeleton
- Paw patrol underwater rescue toys
- Radeon 480
- Minecraft prestonplayz wallpaper
- James 1:13 niv
- Rwby trailer
- Pro rocket league car designs
- Ceremony cards wedding
- Agent fishstick
- Look clipart

## fix

Main Content

### Description

example

rounds object to the nearest integer in the direction of zero and returns the result in object .

### Examples

collapse all

### Use on a Signed Object

The following example demonstrates how the function affects the properties of a signed object with a word length of 8 and a fraction length of 3.

The following example demonstrates how the function affects the properties of a signed object with a word length of 8 and a fraction length of 12.

### Compare Rounding Methods

The functions , , and differ in the way they round objects:

The function rounds values to the nearest integer toward positive infinity.

The function rounds values to the nearest integer toward zero.

The function rounds values to the nearest integer toward negative infinity.

This example illustrates these differences for a given input object .

### Input Arguments

collapse all

### — Input array

scalar | vector | matrix | multidimensional array

Input array, specified as scalar, vector, matrix, or multidimensional array.

For complex objects, the imaginary and real parts are rounded independently.

does not support objects with nontrivial slope and bias scaling. Slope and bias scaling is trivial when the slope is an integer power of 2 and the bias is 0.

**Data Types: ****Complex Number Support: **Yes

### Algorithms

and have the same object and property.

When the property of is , , or , the of is the same as that of .

When the fraction length of is zero or negative, is already an integer, and the of is the same as that of .

When the fraction length of is positive, the fraction length of is , its sign is the same as that of , and its word length is the difference between the word length and the fraction length of , plus one bit. If is signed, then the minimum word length of is . If is unsigned, then the minimum word length of is .

### Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

### HDL Code Generation

Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

**Introduced in R2008a**

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

### How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Contact your local office

**8567**8568 8569