How to match matrix elements for a condition?

6 Ansichten (letzte 30 Tage)
piston_pim_offset
piston_pim_offset am 22 Apr. 2024
I am working on App Designer. I have a matrix and want to match the elements 3 by 3 with a condition. The condition is that addition of 3 elements must be near at a value.
I already can match them by making the addition from minimum to maximum, but it does not seem to be optimal. So, I want them to be around a value.
  2 Kommentare
Dyuman Joshi
Dyuman Joshi am 22 Apr. 2024
Use tolerance to compare -
in = [3 3.3 3.6 3.9];
check = 3.5;
tol = 0.3;
out = abs(in-check)<tol
out = 1x4 logical array
0 1 1 0
piston_pim_offset
piston_pim_offset am 22 Apr. 2024
I'll try right now

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 23 Apr. 2024
Bearbeitet: Torsten am 23 Apr. 2024
If A becomes larger, this brute-force way of solving will become intractable.
A = [15 25 36 17 48 59 31 64 18 21 97 84 31 64 15];
target = 100;
C = nchoosek(1:numel(A),3);
P = arrayfun(@(i)sum(A(C(i,1:3))),1:size(C,1));
[~,I] = sort(abs(P-target));
sums = arrayfun(@(i)sum(A(C(I(i),:))),1:numel(I));
sums = sums(sums==sums(1));
n = numel(sums);
result = unique(sort(A(C(I(1:n),:)),2),'rows')
result = 2x3
15 21 64 21 31 48
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Weitere Antworten (1)

Hassaan
Hassaan am 22 Apr. 2024
Assuming a 1-dimensional array. Will check for sums of three consecutive elements that are close to a given value (target_sum) within a tolerance (tol).
% Example matrix (1D array)
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
% Target sum to match
target_sum = 15;
% Tolerance for matching sum
tol = 0.5;
% Length of the array
n = length(A);
% Pre-allocate the logical output array for matched cases
matches = false(1, n-2); % (n-2) because the last two elements can't start a triplet
% Iterate over the array to find matching triplets
for i = 1:n-2
% Calculate the sum of the current and next two elements
current_sum = sum(A(i:i+2));
% Check if the current sum is within the tolerance of the target sum
if abs(current_sum - target_sum) <= tol
matches(i) = true;
end
end
% Print matched triplets and their indices
matched_indices = find(matches);
for idx = matched_indices
fprintf('Match found at indices [%d, %d, %d]: [%d, %d, %d]\n', idx, idx+1, idx+2, A(idx), A(idx+1), A(idx+2));
end
Match found at indices [4, 5, 6]: [4, 5, 6]
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  5 Kommentare
Torsten
Torsten am 23 Apr. 2024
Bearbeitet: Torsten am 23 Apr. 2024
A = [15 25 36 17 48 59 31 64 18 21 97 84 31 64 15];
C = nchoosek(1:numel(A),3);
P = arrayfun(@(i)sum(A(C(i,1:3))),1:size(C,1));
[B,I] = sort(abs(P-100));
A(C(I(1),:)) % One of the six best combinations
ans = 1x3
15 64 21
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
arrayfun(@(i)sum(A(C(I(i),:))),1:numel(I))
ans = 1x455
100 100 100 100 100 100 99 99 101 99 101 99 99 99 102 102 98 102 102 98 97 97 97 97 97 103 97 103 97 104
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
piston_pim_offset
piston_pim_offset am 23 Apr. 2024
@Torsten that's almost what I was looking for. Is there a way to eliminate the emenents used for the next iteration?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing 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