Help me improve my code about logical and Relational

3 visualizaciones (últimos 30 días)
Stephen Ken Lantapon
Stephen Ken Lantapon el 6 de Jul. de 2021
Comentada: Walter Roberson el 6 de Jul. de 2021
%Given the value of A;
A = [ 2 3 4 5 6 2 4 5 6 7 4 2 4 5 6 7 4 2 4 3 2 4 6 7 5 7 8 5 3 5 6 8 5 3 9 8 7 6 8 9 0 8 7 6 7 8 9 0 7 6 5 6 7 8 9 0 8 7 5 3 4 5 6 6 7 3 4 5 5 5 2 2 2 2 2 2 2 3 7 8 9 0 9 8 6 5 4 3 2 5 7 8 9 7 5 4 3 4 5 7 8 9 8 7 6 5 4 7 6 5 4 6 7 8 9 8 7 6 5 6 7 8 8 8];
%Q1: Determine whether each of the items are greater than 6. Test each element whether each element is greater than 6, then place answer as A1.
A1 = A(A>6);
%Q2: Determine whether each of the items are greater than 8 or lower than 3. Test each element , then place answer as A2.
A2 = A(A>8 | A<3);
%Q3: Determine how many elements in A are equal to 2. Place answer as A3.
A3 = sum(A==2);
%Q4: Determine how many elements in A are divisible by 3. Place answer as A4.
A4 = sum(mod(A,3)==0);
%Q5: Identify the positions of the non-zero numbers in the list A. Place answer as A5.
[~,A5] = find(A~=0);
%Q6: Identify the positions of the numbers between 5 and 8 inclusive. Place answer as A6.
[~,A6] = find(A>=5 & A<=8);
%Q7: Identify the positions of the numbers lower than 4. Place answer as A7.
[~,A7] = find(A<4);
%Q8: List down the elements even numbers as they appear on the list. Place answer as A8.
A8 = A(mod(A,2)==0);
B= mod(1:4:124*4,9);
%Q9: Compare A and B. Is there a value in A are less than its corresponding element in B. Save the count as A9.
[~,A9_postion] = find(A<B);
A9 = A(1,A9_postion);
%Q10: (True or False) All the elements in A are greater than B. Save the answer as A10.
A10 = A>B;
if(sum(A10)==124) %means it will check if the logical expression gives 1 for all 124 elements in A and if all are 1 then sum will be = 124, else if one 0 is there means all element of A are not greater than B
disp('True')
else
disp('False')
end
the result of my code are as follows
  • Assessment result: incorrectA1Variable A1 must be of data type logical. It is currently of type double. Check where the variable is assigned a value.
  • Assessment result: incorrectA2Variable A2 must be of data type logical. It is currently of type double. Check where the variable is assigned a value.
  • Assessment result: correctA3
  • Assessment result: incorrectA4Variable A4 has an incorrect value.
  • Assessment result: correctA5
  • Assessment result: correctA6
  • Assessment result: correctA7
  • Assessment result: correctA8
  • Assessment result: incorrectA9Variable A9 must be of data type logical. It is currently of type double. Check where the variable is assigned a value.
  • Assessment result: incorrectA10Variable A10 must be of size [1 1]. It is currently of size [1 124]. Check where the variable is assigned a value.
  • Assessment result: correctF1
  • Assessment result: incorrectF2The submission must contain the following functions or keywords: any, all
  • Assessment result: correctF3

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Jul. de 2021
You are not asked to select the elements of A that are greater than 6. You are asked to test each of the elements and the result of the test is what should go into A1.
  2 comentarios
Stephen Ken Lantapon
Stephen Ken Lantapon el 6 de Jul. de 2021
can you provide a code?
Walter Roberson
Walter Roberson el 6 de Jul. de 2021
A1 = false(size(A)) ;
for K = 1 : numel(A)
if A(K) > 6
A1(K) = true;
end
end
This is deliberately inefficient code, but try it and see if the grading system accepts the results. If it does then you will have an example output to match against when you experiment with the much more efficient techniques.
One of the first rules of programming: Get it working and debugged first. Worry about efficiency and "cleanness" afterwards, when you have an example implementation to compare against to be sure that the alternative methods produce the same solution. Getting it working and debugged first will provide you with a lot of information about what has to go into any rewriten version. It is very common that when you start implementing something that you do not understand all of implications of the requirements.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by