how to replace consecutive 4 elements of an array with the largest element among every 4 consecutive element ?

1 Ansicht (letzte 30 Tage)
a=[12 58 16 56 23 7 8 45 53 56 12 32 65 12 23 54];
b=zeros(16,1)
for i=1:4:16
for i=1:1:4
if (a(i)>=a(i+1) && a(i)>=a(i+2) && a(i)>=a(i+3));
b(i)=a(i);
b(i+1)=a(i);
b(i+2)=a(i);
b(i+3)=a(i);
elseif (a(i+1)>=a(i) && a(i+1)>=a(i+2) && a(i+1)>=a(i+3));
b(i)=a(i+1);
b(i+1)=a(i+1);
b(i+2)=a(i+1);
b(i+3)=a(i+1);
elseif (a(i+2)>=a(i) && a(i+2)>=a(i+1) && a(i+2)>=a(i+3));
b(i)=a(i+2);
b(i+1)=a(i+2);
b(i+2)=a(i+2);
b(i+3)=a(i+2);
else
b(i)=a(i+3);
b(i+1)=a(i+3);
b(i+2)=a(i+3);
b(i+3)=a(i+3);
end
end
end
b should be [58 58 58 58 45 45 45 45 56 56 56 56 65 65 65 65]

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Mär. 2016
The other two answers are good for what you asked. One other way I'll just mention is that you can use blockproc() to move along in "jumps" of 4 elements and take the max. Although slightly more compact, it's more cryptic (as compact code often is) and not as easy to understand what it's doing so I won't give it.
Another, related, option (that you didn't ask for but I'll just lay out there for completeness) is to use imdilate() to do "morphological dilation". The constraint though is that it moves along one element at a time as it slides the 4-element window along.
b = imdilate(a, true(1,4));
Again, the window there slides by 1, not in jumps of 4. It's basically a sliding local max filter.

Weitere Antworten (3)

Star Strider
Star Strider am 26 Mär. 2016
Bearbeitet: Star Strider am 26 Mär. 2016
MATLAB makes this straightforward:
a=[12 58 16 56 23 7 8 45 53 56 12 32 65 12 23 54];
M = reshape(a, 4, []);
Mcolmax = max(M);
b = reshape(ones(4,1) * Mcolmax, 1, [])
b =
58 58 58 58 45 45 45 45 56 56 56 56 65 65 65 65
EDIT It will work for your (8760x1) vector without modification. I tested it on ‘a’ transposed to a column to be certain it will work with both row and column vectors. The only restriction is that your vector length has to be an integer multiple of 4.
  4 Kommentare
Star Strider
Star Strider am 27 Mär. 2016
I don’t have your vector so I can’t check my code with it, but there is either something wrong with your implementation of it or with your interpretation of its results.
It’s a long vector. Are you certain the Command Window didn’t cut off the first two rows, and that you would see them if you scrolled down? The reason is that I don’t see any leading blank lines or the ‘>>’ Command Prompt in your output.
The solution is to use fprintf to print it to a text file, and then look at it in ‘notepad’ or other text editor. If you look at the results using that technique, all the values will have the required 4 occurrences.

Melden Sie sich an, um zu kommentieren.


Ced
Ced am 26 Mär. 2016
One possibility would be the following:
1. reshape your vector as a 4xN matrix
a=[12 58 16 56 23 7 8 45 53 56 12 32 65 12 23 54];
A = reshape(a(:),4,[]);
2. get the maximum of each column ( which is a row )
max_col = max(A,[],1);
3. copy the values 4 times and shape as a vector
A = reshape(max_col,4,1);
a = A(:)';
Alternatively, you can use the kronecker function in the last step:
a = kron(max_col,ones(1,4));
or all at once:
a = kron(max(reshape(a(:),4,[]),[],1),ones(1,4));
Cheers
PS: The code above return a row vector in the end (as in your example). If your next application has a 8760*1 vector as you say, you need to transpose the last result (or directly construct a column vector).

Steven Lord
Steven Lord am 26 Mär. 2016
If you're using release R2016a or later and you want to do this for a vector, take a look at the movmax function.
  1 Kommentar
Image Analyst
Image Analyst am 26 Mär. 2016
I didn't know about this. I guess I should look at release notes. It looks like imdilate() but with some extra options about how to handles nans and edge effects. It also seems like they overlooked the easy-to-implement movement in "jumps" as this seems like it only moves along one element at a time with no opportunity to move by some specified jump distance.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by