Matlab incorrect computation result [closed] - matlab

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I got a few Matlab code to play. But the answer is not correct:
x = linspace(-pi, pi, 10)
sinc = #(x) sin(x) ./ x
sinc(x) // wrong result occurs at here.
The expected result as below:
ans =
Columns 1 through 6:
3.8982e-17 2.6306e-01 5.6425e-01 8.2699e-01 9.7982e-01 9.7982e-01
Columns 7 through 10:
8.2699e-01 5.6425e-01 2.6306e-01 3.8982e-17
real result:
ans =
Columns 1 through 3
0.000000000000000 0.263064408273866 0.564253278793615
Columns 4 through 6
0.826993343132688 0.979815536051016 0.979815536051016
Columns 7 through 9
0.826993343132688 0.564253278793615 0.263064408273866
Column 10
0.000000000000000
details: My OS is arch linux,
Matlab is downloaded through official website.
matlab version is 2015b

The expected result and the real results you present are identical as far as I can see.
The only difference is the notation: normal vs scientific.
With format short you can switch to scientific notation and get identical results with identical formatting.

Related

Problem with displaying the elements of a cell array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have tried to create vector matrix array, to add integer elements after their arrival.
a={[1 1 1 1 1]; [3 3 3 3 3 3 3 3 3]; [4 4]; [5]};
print(a);
That code gives me this error:
You should take a look at the documentation for print. It is used to:
Print figure or save to specific file format
What you want is either disp which is used to
Display value of variable
Or fprintf which is used to:
Write data to text file (which can be the console)
Or even simpler: Just write
a % Note the absence of ';'
Upon encountering an operation without semicolon, MATLAB aromatically displays the result in the console. So this is enough to print you variable.

The index ranging in Matlab matrices [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to know what this line of code does.
ind_x = [1,3:5:size(paths,2)]
What would ind_x contain after this line? I already know that size(paths,2) means the size of second dimension of paths matrix.
3:5:size(path,2) returns a vector which starts from 3 to size(path,2) with steps 5. For example, if size(path,2) is equal to 20, the result would be:
ind_x
[1 3 8 13 18]
As you can see it counts from 3 to size(path,2) with step size 5 (3, 8, 13, ...).

element-wise matrix min in MATLAB [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is weird.
I have a giant 10000x1 vectors (call it B), and 10000x1 vector (call it A) that only contains all ones.
I want to do element-wise min comparison of B and A.... such that min(B(1),A(1)), min(B(2),A(2)).... and so on.
So C=min(B,A)
I have B(1) = 0.85, and B(2) = 1.25, when we are doing this min, I expect that C(1)=0.85, and C(2)=1.
However, the resulting C matrix has: C(1)=0.085, and C(2)=0.125. Why is this?
You could also do
C = ((A+B) - abs(A-B))/2;
Without seeing the full code and values it is hard to see what exactly is the problem, but obviously something is wrong or different than you describe.
See this example for how it would be done according to your description:
A = ones(5,1);
B = A*1000;
B(1)=0.85;
B(2)=1.25;
C = min(B,A)
Will produce:
C =
0.8500 1.0000 1.0000 1.0000 1.0000
Changing the 5 into 10000 will of course not change the first two values of C.

In matlab why is the 1st digit in a binary notation being discarded? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i try to display binary notations which start with a zero in the 1st bit position, matlab discards the zero and displays only the other 7 bits. How do I display the 1st position too?
ex: when i try to display "01101111", matlab displays it as "1101111", but I need the 1st bit position value also. Can some one please help.
In Matlab, to display the bit representation of a number you need to convert it into a string with dec2bin().
So, if you have x = 111, it's binary representation is:
dec2bin(111)
ans =
1101111
which retains only the significant bits. To force an 8-bit representation use:
dec2bin(111,8)
ans =
01101111
Note, how the result will be a string. If you want to retrieve bits in numeric format, then use bitget():
bitget(111,8:-1:1)
ans =
0 1 1 0 1 1 1 1
Basically, if your need is purely visual, use dec2bin2() otherwise for manipulating bits, use the bit-wise operations functions, which accept and return numeric types.

how can I get a complete vector of residuals from an ARX model [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used ARX function then RESID function from the System Identification Toolbox, but the resulting residuals are:
0
0
0
5
6
8
7
8
the number of zeros=the number of lags, I need a complete vector of residuals
An AR model of order N needs the previous N values to predict the next one, which is why the first N are not predicted. You can always pad the vector at the beginning (either by replication or zeros), example:
load twotankdata
order = 5;
m = arx(y, order);
r = resid([y(1:order);y], m);
r = r(order+1:end);