How to compare two matrices on MatLab? [closed] - matlab

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.

Related

Two dimensional matrix in Perl [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
Newbie's question: What does $matrix[$row][4]=1; mean in a code? The row and column are known to me. Why is it made equal to 1?
The command will set the cell at position $row,4 with the value 1
= is the assignment operator in Perl. It assigns the value 1 to the cell at the coordinates $row, 4.
The comparison operators for equiality are eq for strings and == for 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.

getting occurrence of alphabets in a string, in 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
i have to do huffman coding on a message read from a file,in Matlab.For that i have to find the probability occurrence of each alphabet in that message.Using that frequency i have to do huffman encoding.Can you please specify how to read a message from the file and store it as a string for the same purpose..Can anyone help me to solve this
What you need is a histogram count:
counts = histc(lower(x), 'a':'z');
where the output count contains the number of occurences for each letter in the message string x. For instance, the first element count(1) corresponds to the number of occurrences of a, count(2) corresponds to the number of occurrences of b, etc...
Also note that this x is converted to lowercase o make the counting case insensitive.

To find an element with nearest lower and greater value in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I have two sorted matrices A and B. For all values in column 1 of A, how do I find nearest lower and greater value in matrice B? (no treshold)
I would use interp1, but in the opposite way it's normally used. Consider your B matrix a look-up table. You're trying to look up the index of an element given it's value. For example:
% Sample data
B = sort(rand(10,1));
A = sort(rand(5,1));
idx = interp1(B, 1:size(B), A, 'linear', 'extrap');
idx will be a double-precision value that shows the location of each element of A in B. 2.2, for instance, says that the value is between element 2 and element 3. In fact, it's 20% of the way from element 2 to element 3. So floor(idx) is the lower element, and ceil(idx) is the higher.
Caveats: duplicate elements in B will create a problem. And the edge conditions might be messy. You'll have to work those out yourself. See what happens with an A element that's outside the range of B.

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