MATLAB Help - Aligning Two Matrices

  • Context: MATLAB 
  • Thread starter Thread starter Snk_majin
  • Start date Start date
  • Tags Tags
    alignment Matlab
Click For Summary
SUMMARY

The discussion centers on a MATLAB function named Score, which calculates the similarity between two matrices, returning 1 for matches and -1 for non-matches. The user encountered an error related to the use of the multiplication operator with logical values. A solution was provided, suggesting the conversion of logical results to integers using the int8 function to avoid the error. The corrected code effectively resolves the issue by ensuring compatibility with the multiplication operator.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of matrix operations in MATLAB
  • Knowledge of logical indexing and boolean expressions
  • Basic concepts of error handling in MATLAB
NEXT STEPS
  • Explore MATLAB's int8 function for type conversion
  • Learn about logical indexing and boolean operations in MATLAB
  • Study matrix multiplication rules in MATLAB
  • Review error handling techniques in MATLAB programming
USEFUL FOR

Beginner MATLAB programmers, data analysts working with matrices, and anyone troubleshooting matrix operations in MATLAB.

Snk_majin
Messages
2
Reaction score
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
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   Reactions: DrClaude
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;
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K