MATLAB Help - Aligning Two Matrices

In summary, the conversation is about a user having trouble with a function called score in MATLAB. They are getting an error related to using the * operator on boolean expressions. The expert suggests using boolean operators or converting the logical result to an integer. The user also asks for learning resources for MATLAB.
  • #1
Snk_majin
2
0
Hi all

I'm new to MATLAB and am having trouble with a certain function, called score. I've looked at this for a while now and can't see where I am going wrong?!

The function scores the similarities of two matrices,
1 for a match
-1 for a non match

the code:


function s = Score(x,y,d)

% Score matirx for x and y in {1,2,3,4}
% Mismatch penalty = d

s = ((x==1)'*(y==1)+(x==2)'*(y==2)+(x==3)'*(y==3)...
+(x==4)'*(y==4))*(1+d) - d;

input:

x = ceil(4*rand(1,5))
y = ceil(4*rand(1,7))
[0 y; x' Score(x,y,1)]

..and that error...

? Error using ==> _times_transpose
Function '*' is not defined for values of class 'logical'.

Error in ==> C:\MATLAB6p5p1\work\Score.m
On line 6 ==> s = ((x==1)'*(y==1)+(x==2)'*(y==2)+(x==3)'*(y==3)...


.. i;m not sure what it is?! I'm sure its something dumb.

Can you suggest a learning resource, my curernt approach is to go through online tutorials.

Thanks for your time

Snk_majin
 
Physics news on Phys.org
  • #2
Each of the expressions x == 1 and so on, return a boolean value, either true or false. The * operator cannot be used for boolean expressions.
 
  • Like
Likes DrClaude
  • #3
On would need to use boolean operators instead, which here would be and (&&):
Matlab:
(x==1)' && (y==1)
However, that won't help with the summation. The easiest is to convert the logical result to an integer instead:
Matlab:
s = (int8(x==1)'*int8(y==1)+int8(x==2)'*int8(y==2)+int8(x==3)'*int8(y==3)...
+int8(x==4)'*int8(y==4))*(1+d) - d;
 

What is the purpose of aligning two matrices in MATLAB?

The purpose of aligning two matrices in MATLAB is to ensure that they have the same size and shape, so that mathematical operations can be performed on them. This is important because MATLAB operates on matrices, and any mismatch in size or shape can result in errors or incorrect results.

How can I align two matrices of different sizes in MATLAB?

To align two matrices of different sizes in MATLAB, you can use the built-in functions such as zeros or ones to create a new matrix of the desired size, and then copy the values from the original matrices into the new matrix. Alternatively, you can also use the reshape function to change the size and shape of a matrix.

Can I align matrices with different dimensions in MATLAB?

Yes, you can align matrices with different dimensions in MATLAB. However, you will need to first understand the rules of matrix operations in MATLAB, such as the concept of singleton dimensions. You can also use the bsxfun function to perform operations on matrices with different dimensions.

What happens if I try to align two matrices with different dimensions in MATLAB?

If you try to align two matrices with different dimensions in MATLAB, you may encounter an error. This is because MATLAB strictly follows the rules of matrix operations, and a mismatch in dimensions can lead to errors. However, if the matrices have compatible dimensions, they can be automatically expanded or compressed to match each other.

Is there a function in MATLAB to automatically align two matrices?

Yes, there is a function in MATLAB called align which can automatically align two matrices. However, this function is not a built-in function and needs to be downloaded from the MATLAB File Exchange. It works by padding or cropping the matrices to match their size and shape.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top