Research of an element in an array

39 views (last 30 days)
Luigi Bucci
Luigi Bucci on 24 Jan 2022
Edited: Luigi Bucci on 24 Jan 2022
%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
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.
Luigi Bucci
Luigi Bucci on 24 Jan 2022
Edited: Luigi Bucci on 24 Jan 2022
%Inizio esercizio
disp ('Inizio esercizio')
%Generazione di una matrice con 5 righe e n colonne. Il numero n delle
%colonne dev'essere maggiore di 5. Successivamente verranno estratti i
%vettori riga R1 e R3 e verrà fatta una somma dei due vettori in un vettore
%S. Infine verrà ricercato un elemento X all'interno del vettore S per
%capire se fa parte o mento degli elementi del vettore.
%Generazione matrice
disp ('Inizio generazione matrice. M=Righe, N=Colonne');
M=5;
N=input ('N');
for i=1:5
for j=1:N
if (N>5)
disp ('Numero colonne matrice corretto. Inizio generazione matrice')
A(i,j)=input ("Inserire l'elemento: ");
disp ('Stampa matrice A')
disp (A);
else
disp ('Numero colonne matrice insufficiente. Riavviare il programma')
break
end
end
end
%Estrazione vettore riga R1 dalla matrice A
disp ('Estrazione vettore riga R1 dalla matrice A')
R1=A(1,:);
disp ('Stampa vettore riga R1')
disp (R1)
%Estrazione vettore riga R3 dalla matrice A
disp ('Estrazione vettore riga R3 dalla matrice A')
R3=A(3,:);
disp ('Stampa vettore riga R3')
disp (R3)
%Somma vettori riga R1 e R3
disp ('Somma dei vettori riga R1 e R3 in un vettore S')
S=R1+R3;
disp ('Stampa vettore S')
disp (S)
%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
%Fine Esercizio
disp ('Fine Esercizio')
This is the complete program.

Sign in to comment.

Accepted Answer

Voss
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
  1 Comment
Luigi Bucci
Luigi Bucci on 24 Jan 2022
It works now. Thanks for your help and good evening.

Sign in to comment.

More Answers (1)

David Hill
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
  1 Comment
Luigi Bucci
Luigi Bucci on 24 Jan 2022
Edited: Luigi Bucci on 24 Jan 2022
There should be also multiple matches. Thanks for the help. And Good evening

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!