Unable to perform assignment because the because the indices on the left side are not compatible with the right side

2 views (last 30 days)
Hi all,
I had an error regarding assigning values to variables. I was trying to execute a simple program but it always gives me an unable to perform assignemnt error.
A=[ 1,5;2,6;1,5;1,4;3,7;6,5;6,1];
m=7;
for n=1:m
if (A(n,1)== A(:,1))
b(n,1)=find(A(:,1)== A(n,1));
end
end
Can somebody let me know where am I going wrong in this.
Thank you

Answers (2)

Travis Malouf
Travis Malouf on 22 Nov 2019
So, when I execute your code as supplied, it runs to completion without errors in Matlab 2019b.
A=[ 1,5;2,6;1,5;1,4;3,7;6,5;6,1];
m=7;
for n=1:m
if (A(n,1)== A(:,1))
b(n,1)=find(A(:,1)== A(n,1));
end
end
However, I don't think your for loop is accomplishing what you want. The statement
(A(n,1)== A(:,1))
Returns a 7x1 array. That means the code inside your if-statement never gets executed.
However, if I run just the b(n,1)... part, then I get your error. The reason is that the
find(A(:,1)== A(n,1))
part is not always a scalar. For some values of n, you will have an array of values.
So, the reason for your error is that you are attempting to put the array returned by find() into the scalar b(n,1).
  2 Comments
Travis Malouf
Travis Malouf on 22 Nov 2019
I'm not 100% clear on what you are trying to accomplish with your b variable. The motivation will determine potential solutions.
With the line:
if (A(n,1)== A(:,1))
Are you trying to check whether elements in the first column have the same value? If so, I would suggest the any() function.
Then, with
b(n,1)=find(A(:,1)== A(n,1));
the motivation needs to be sorted. The way the right side of = is written, you are looking for all indices of column 1 of A that match the nth item in column 1 of A....Another way to say that: You are finding matching numbers in column 1 of A.
So, what do you hope to accomplish with b?

Sign in to comment.


Rashmi Mohan Kumar
Rashmi Mohan Kumar on 23 Nov 2019
I was actually working on a set of array elements, 50X2. The 1st column gives the frame numbers and the second column is the slot numbers. So each column as random numbers and they are repeating. So what I am trying to do in my experiment is as follows. For example I have the following matrix:
A= [ 1 3
4 7
1 6
4 7
1 6
5 3
4 7 ]
Now I need my code to check for all the repeating numbers in column 1. From the above matrix the values 1 and 4 repeats. For value 1, it repeats on row numbers 1, 3 and 5, I need the code to check for the corresponding values from the 2nd column but on the same rows, where value 1 is repeated in the 1st column. This has to be done for all the elements in column 1. After getting the corresponding numbers from column 2, the code should check if those numbers are also same, meaning from the matrix above for value 1 on row numbers 1, 3 and 5 the corresponding values from the 2ndcolumn are 3, 6 and 6. The code should check if these numbers are also same, if not ignore it and if it is same consider it only if it has repeated twice and if it as repeated more than twice then place a value equal to 1 on all those rows but in the 3rd column of the matrix. So basically it should ignore 3 and give the row numbers of the value 6, since it is repeated only twice that is row numbers 3 and 5, but if you see for a value of 4 in the 1stcolumn, the corresponding element in the 2nd column is 7 which even though is the same number it is repeated more than twice so it should be ignored and a value 1 should be placed on all the rows where it is 7 but in the 3rd column. And I wanted to implement this using looping instead of using functions for all the elements in the matrix.
Can anyone help me with this?
  1 Comment
Travis Malouf
Travis Malouf on 30 Nov 2019
I don't know that anyone is going to write the code for you, but I can make some suggestions.
Without actually having taken the time to implement any of this, I think you can solve your problem using two for loops. The first one will find the duplicates in column 1.
The second for loop would look at all the duplicate indices found in loop/column 1 and see if the column 2 values match.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!