Operation with logical data. Which is better?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
pipor
el 6 de Sept. de 2023
Respondida: Riya
el 7 de Sept. de 2023
n=5;
a=[1 9 0 2 3];
a.*(a<n)
(a.*(a<5))>0 %A
logical(a.*(a<5)) %B
Is a better solution A or B? Or does another solution exist?
0 comentarios
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 6 de Sept. de 2023
Those are different things you're showing. "a.*(a<5)" is a double vector. Your A only shows positive values, while B shows non-zero elements, both positive and negative value locations. Note the difference in my modified code
n = 5;
a = [1, 9, 0, -2, 3];
theProduct = a.*(a<n)
theProduct > 0 %A
logical(theProduct) %B
So it depends on what you want to do.
Most of the time, I just use the logical operation (like < or > etc.) to product a logical variable. I rarely call the logical function explicitly.
Riya
el 7 de Sept. de 2023
Hello pipor,
As per my understanding, you want to know which function is better to use.
Please note that, in terms of space and time complexity, both Solution A and Solution B have the same complexity.
Space Complexity:
Both solutions require additional space to store the intermediate array `a.*(a<5)`. The space complexity for both solutions is O(n), where n is the length of array `a`. This is because the intermediate array will have the same size as the input array `a`.
Time Complexity:
Both solutions involve element-wise multiplication and comparison operations. The time complexity for both solutions is also O(n), where n is the length of array `a`. This is because both solutions require iterating over each element of the array once.
Therefore, in terms of space and time complexity, both Solution A and Solution B are equivalent. You can choose either solution based on your preference for code readability and simplicity.
To analyse the time taken by both solutions, you can use the `tic` and `toc` functions in MATLAB to measure the execution time. Here's an example of how you can use `tic` and `toc` to compare the time taken by Solution A and Solution B:
n = 5;
a = [1 9 0 2 3];
% Solution A
tic;
resultA = (a.*(a<5)) > 0;
timeA = toc;
% Solution B
tic;
resultB = logical(a.*(a<5));
timeB = toc;
disp("Solution A:");
disp(resultA);
fprintf("Time taken by Solution A: %.6f seconds\n", timeA);
disp("Solution B:");
disp(resultB);
fprintf("Time taken by Solution B: %.6f seconds\n", timeB);
By running this code, you will get the output showing the results of both solutions and the time taken by each solution. The time taken may vary depending on your hardware and other factors, but this will give you an idea of the relative time difference between the two solutions.
For more information regarding tic and toc function you can refer the following links:
- https://www.mathworks.com/help/matlab/ref/tic.html?searchHighlight=tic%20toc&s_tid=srchtitle_support_results_1_tic%20toc
- https://www.mathworks.com/help/matlab/ref/toc.html?searchHighlight=tic%20toc&s_tid=srchtitle_support_results_2_tic%20toc
I hope it helps!
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!