Get range of integer values from adjacent column elements (integers) for multiple rows.
3 次查看(过去 30 天)
显示 更早的评论
Note: (inadvertent early submission with question incomplete)
Dear MATLAB Community:
There is perhaps an even more elegant solution for what I am trying to accomplish, but now that I have gone about it in a particular manner, I am curious if I can use this approach without implementing the for loop that currently seems necessary. I think it's very possible that I might be missing something syntactically, or that I am unware of certain limitation to MATLAB's indexing properties.
One might use this code if they were generating a grating stimulus (interchaginging white and black bars with equal spatial frequency) for display in an visual cognition experiment.
Current solution with desired output "requires" for loop as follows:
% stimulus parameters
img_matrix = ones(300,300); %this could be any given size
seg = 20; % same
% generate the column indices for 'filling' given spatial frequency of 20
col_fill_indx = [];
col_indx = 1:seg*2:length(img_matrix);
col_indx = transpose([col_indx; col_indx + range(1:seg)]);
p = length(col_indx);
for i = 1:p
col_fill_indx = [col_fill_indx; col_indx(i,1):col_indx(i,2)];
end
% 'fill' in the desired column indices
img_matrix(:, col_fill_indx) = zeros;
figure, imshow(img_matrix);
The solution I initially believed would work, which led to the implementation of the for loop:
% stimulus parameters
img_matrix = ones(300,300); %this could be any given size
seg = 20; % same
% generate the column indices for 'filling' given spatial frequency of 20
col_fill_indx = [];
col_indx = 1:seg*2:length(img_matrix);
col_indx = transpose([col_indx; col_indx + range(1:seg)]);
col_fill_indx = col_indx(:,1):col_indx(:,2);
% also tried: col_fill_indx = col_indx(1:8,1):col_indx(1:8,2)
% 'fill' in the desired column indices
img_matrix(:, col_fill_indx) = zeros;
figure, imshow(img_matrix);
col_fill_indx only returns the range for the INITIAL row of the col_indx column values:
1, 2, 3, 4, ..., 20
Is there a work around here that doesn't require a for loop or something else more elaborate than I currently have in place?
Any insights or help would be most appreciated. And if you do happen to have an even more elegant solution, I am open to hearing that as well! However, I want to understand why the latter code (initial solution) I posted doesn't work, as my current understanding of MATLAB indexing suggests that it should.
-Ray
2 个评论
采纳的回答
Bruno Luong
2018-11-25
编辑:Bruno Luong
2018-11-25
This code
n = 10;
seg = 2;
b = mod(0:n-1,2*seg)<seg;
A = repmat(1-b,n,1)
Gives this output:
A =
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
0 0 1 1 0 0 1 1 0 0
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!