for loop only through certain elements of an array
显示 更早的评论
Hello all,
I would like to do this more time efficiently. Lets say I have a 4D (xn,yn,zn,N) data array and a 3D (xn,yn,zn) mask array. I would like to go through 4D array and calculate something from the numbers in the 4th dimension, but I want only elements from the mask. Is there a way of doing this more efficiently, than going through xn*yn*zn elements and checking if they are withing the mask?
This is the brute force way:
for x=1:xn
for y=1:yn
for z=1:zn
if mask(x,y,z)==1
do something with data(x,y,z,:);
end
end
end
end
Thank you.
Edit: I think I need to explain the task at hand better. The 4D data is several 3D images combined along the 4th dimension, which corresponds to certain image acquisition parameters. I want to do
f=fit(Par, data(x,y,z,:), 'exp1');
for each image voxel from the mask.
6 个评论
Adam
2017-11-22
You can just do a find on your mask, store the indices and loop over only those instead. A lot depends on the density of 1s in the mask as to how fast one approach would be over another.
Greg
2017-11-22
Depending on the something you're calculating, you could set all masked values to NaN. Many statistics functions (min, max, mean, etc.) have the 'omitnan' flag.
Renat
2017-11-22
"I dont know how to access elements in 3D array with 1D indices." -- You just do it.
a = cat(3,[1,3;2,4],[5,7;6,8]);
b = a(7);
The value of "b" is the seventh element of "a" - 7. Look up MATLAB's documentation on Linear Indexing, as well as functions like sub2ind.
Also, I'm not an expert in exponential fits. Would NaN values skew the result of the fit?
Renat
2017-11-23
Image Analyst
2017-11-23
So you're doing something like this:
for k = 1 : xn*yn*zn
if mask(k)
% do something with data(x,y,z,:);
% Now need x, y, and z so need to use ind2sub().
[x, y, z] = ind2sub(size(data), k);
thisData = data(x, y, z);
end
end
Note that arrays not not normally indexed as (x, y, z) though. They're indexed (row, column, z) which is (y, x, z). So the above code just assumes that x is the row, which is fine, just realize that it's not the x as you usually think of it.
回答(1 个)
Image Analyst
2017-11-22
Try looping over the 4th dimension
for n = 1 : N
this3DArray = data(:,:,:,n);
dataOnlyWithInMask = this3DArray(mask); % 1-D vector.
% Now do something with dataOnlyWithInMask .
end
2 个评论
Renat
2017-11-22
Image Analyst
2017-11-22
If it's something like a video where you have color images, and then a bunch of them over time, then you need to extract just one pixel's color channel over time. So you'd do this:
redChannelOverTime = squeeze(data(y, x, 1, :));
Note how y and x area flipped because y is rows which is the first index. I think this will provide a 1-D array along the time dimension. Then do whatever fitting you want to it.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!