Unable to perform assignment because the because the indices on the left side are not compatible with the right side
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
Travis Malouf
il 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 Commenti
Travis Malouf
il 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?
Rashmi Mohan Kumar
il 23 Nov 2019
1 Commento
Travis Malouf
il 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.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!