element-wise matrix min in MATLAB [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 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.

Related

Matlab incorrect computation result [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 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.

why repmat(1,(1,10)) does not work 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 7 years ago.
Improve this question
Here is the code.
>> a=ones(1,10);
>> b=size(a);
>> repmat(1,b)
ans =
1 1 1 1 1 1 1 1 1 1
>> repmat(1,(1,10))
repmat(1,(1,10))
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Does anyone know why? and why does the error go like that? Thanks.
The expression size(a) returns [1 10], not (1,10). So the equivalent is:
repmat(1, [1 10]);
Here's some helpful documentation:
Matrices and Arrays
The size function
The repmat function
The full equivalent to your example code is repmat(1,size(ones(1,10))). Alternatively you can use a repmat(1,[1,10]), for array construction you have to use [], the round brackets () are for function call and indexing only.
Repmat is used to create an array with n repeating copies of the source array (A). Hence, the second argument is a single scalar defining the number of copies of A, the first argument.
You can use a vector for the second argument, but it defines how many copies the result has in different dimensions. Syntax would be: repmat(1,[1 10])

How to compare two matrices on MatLab? [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
Given a matrix A and a matrix B, how do I compare them element by element so the program returns a third matrix C that shows:
- If the element in A is larger than the one in B, the element in C should be 1.
- If the element in A is smaller than in B, the element in C should be -1.
- If the elements of both matrices are equal, the element in C should be 0.
Hope you can help!
C=zeros(size(A));
C(A>B) = 1;
C(A<B) = -1;
Note that it's never a good idea to do an equality test on floating point numbers.

Returning the last element of a vector with an unknown length [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
How can I retrieve the last element of a vector only, provided that the length of that vector is unknown?
Use the special end keyword:
lastelement = myvector(end);
If the vector is called A, just use A(end).
In this case, use end, like #nispio and #David answered.
But it seems you think that not knowing the length can be a problem, but nope. That's because you can use length(v) if v is a column or row vector, or size(M) if M is a matrix.
Then, to get the last element of your vector, you could use (not recommended):
v(length(v)) if v is a row or a column vector
v(size(v,1)) if v is a column vector
v(size(v,2)) if v is a row vector
But if you use one of them, MATLAB will warn you:
The operation or expression <Indexing> has no evident effect.

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);