How does the colon operator work in a 4d object?

2 views (last 30 days)
MF
MF on 6 Mar 2022
Edited: DGM on 6 Mar 2022
This is a basic question about calling a specific dimension of a 4d object, but I can't find clarity online. I used the read() function to read in a movie object interpretted with MovieReader. If I wanted to isolate the fourth dimension–say, find the mode of the fourth dimension–and subtract all values of the fourth dimension by the mode of that dimension, what would the syntax be? I've pasted what I have below, but I sense it's wrong.
More generally, how would I affect the values of one of the four dimensions without the others being altered? Is the colon a placeholder that tells MATLAB "look here", or am I thinking of it in the wrong way? Thanks, in advance.
Movie = VideoReader("Example_mov.avi");
mov = read(Movie);
% The goal is to have new values replace the old ones in the matrix.
% Note: 300, 300, and 3 are the existing values of the other dimensions
% of the matrix. I don't want these changed from their inital values.
a = mov(300, 300, 3, :);
c = mode(mov(300, 300, 3, :));
d(:,:,:,1) = a - c
  1 Comment
Paul
Paul on 6 Mar 2022
Can the question be clarified? To simplify, let's assume a 2D matrix, rather an than 4D
mov = rand(5,3);
a = mov(5,:) % this is the last row of mov
a = 1×3
0.0336 0.4624 0.6313
c = mode(mov(5,:)) % this is mode of the last row of mov. Could also write c = mode(a)
c = 0.0336
d(:,1) = a - c % subtract c from a and store in d, result is a column vector
d = 3×1
0 0.4287 0.5976
a - c
ans = 1×3
0 0.4287 0.5976
Is d shown above the expected/desired result for the 2D case?

Sign in to comment.

Answers (1)

DGM
DGM on 6 Mar 2022
Edited: DGM on 6 Mar 2022
Your goal isn't to affect the values of a particular dimension. There are no values "of a dimension" it's just the whole array. The thing you're trying to do is to simply orient your operations along a dimension. With many functions, you can simply specify what dimension to operate along.
A = imread('peppers.png'); % a single frame RGB image (MxNx3)
A = cat(4,A,A,A*0.9); % make a 3-frame RGB image (MxNx3x3)
framemode = mode(A,4); % find the mode of the 4D array along dim4 (result is MxNx3)
B = A-framemode; % subtract MxNx3 mode array from MxNx3x3 multiframe image
Note that the final subtraction utilizes implicit array expansion.

Community Treasure Hunt

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

Start Hunting!