Research of an element in an array
39 views (last 30 days)
Show older comments
%Ricerca elemento X nel vettore somma S
disp ('Ricerca elemento X nel vettore somma S')
X=input ('X');
if K==find(S==X)
disp ("L'elemento X si trova nella posizione ")
disp (K)
disp (" del vettore S")
else
disp ("L'elemento X non è presente all'interno del vettore S")
end
This is a part of a program I'm creating as a simulation of an exam of my university, the Politecnico of Bari in Italy.
Here there's a research of an element in an array. After the if, the program stops working. How could I fix it? The error say Unknown or unrecognizable variable K.
The variable K is created when the software find the element in the array.
Thanks for the help.
Regards,
Luigi
P.S. Before writing it, all the sentences written in disp are required by my teacher, cause the absence of them could not help my teacher to understand all the program phases. So, I must write it, as I need to write them the day of the exam to get a high mark and get all the European credits of the university. (In Italian, CFU: Crediti Formativi Universitari).
2 Comments
David Hill
on 24 Jan 2022
What are you trying to compare in the conditional if statement? Give an example of S. With the code shown, there is no S or K defined.
Accepted Answer
Voss
on 24 Jan 2022
Perhaps you mean to do this:
disp ('Ricerca elemento X nel vettore somma S')
X=input ('X');
K = find(S==X);
if ~isempty(K)
disp ("L'elemento X si trova nella posizione ")
disp (K)
disp (" del vettore S")
else
disp ("L'elemento X non è presente all'interno del vettore S")
end
More Answers (1)
David Hill
on 24 Jan 2022
This should work for you as long as there is only going to be a single match or no match
K=find(S==X,1);
if ~isempty(K)
disp ("L'elemento X si trova nella posizione ")
disp (K)
disp (" del vettore S")
else
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!