Filter löschen
Filter löschen

Operation with logical data. Which is better?

1 Ansicht (letzte 30 Tage)
pipor
pipor am 6 Sep. 2023
Beantwortet: Riya am 7 Sep. 2023
n=5;
a=[1 9 0 2 3];
a.*(a<n)
ans = 1×5
1 0 0 2 3
(a.*(a<5))>0 %A
ans = 1×5 logical array
1 0 0 1 1
logical(a.*(a<5)) %B
ans = 1×5 logical array
1 0 0 1 1
Is a better solution A or B? Or does another solution exist?

Akzeptierte Antwort

Stephen23
Stephen23 am 6 Sep. 2023
n = 5;
a = [1,9,0,2,3];
b = a<n & a
b = 1×5 logical array
1 0 0 1 1

Weitere Antworten (2)

Image Analyst
Image Analyst am 6 Sep. 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 = 1×5
1 0 0 -2 3
theProduct > 0 %A
ans = 1×5 logical array
1 0 0 0 1
logical(theProduct) %B
ans = 1×5 logical array
1 0 0 1 1
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
Riya am 7 Sep. 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:");
Solution A:
disp(resultA);
1 0 0 1 1
fprintf("Time taken by Solution A: %.6f seconds\n", timeA);
Time taken by Solution A: 0.007393 seconds
disp("Solution B:");
Solution B:
disp(resultB);
1 0 0 1 1
fprintf("Time taken by Solution B: %.6f seconds\n", timeB);
Time taken by Solution B: 0.001997 seconds
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:
I hope it helps!

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by