Get range of integer values from adjacent column elements (integers) for multiple rows.

4 次查看(过去 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 个评论
Raymond MacNeil
Raymond MacNeil 2018-11-25
编辑:Raymond MacNeil 2018-11-25
If I change the parameters so that the matrix is 10 x 10, and the seg = 2, this is the matrix that is produced:
img_matrix =
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
This is what is produced with the second set of code I have presented, which I thought would have yielded the same result as above:
img_matrix =
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
Desired result is the first matrix presented above, which is achieved with the first code snippet in my original question (using the for loop). My question is this: is the loop necessary, given my particular approach, or is there something else that can be done (modifying my syntax)?
col_indx looks like this:
col_indx =
1 2
5 6
9 10
In my second code snippet, I would have expected that this command input would yield the desired result (as oppossed to the second matrix example in this post with only the first two columns being populated with zeros:
col_fill_indx = col_indx(:,1):col_indx(:,2)
which yields,
col_fill_indx =
1 2
With the FOR loop, the correct output is produced:
col_fill_indx =
1 2
5 6
9 10
Note that because seg = 2 in this particular example, col_indx == col_fill_indx
If seg > 2, then it would become necessary to define col_fill_indx (given my approach, of course). However, the above example still illustrates the main point of my question...I am just partly surprised that col_fill_indx = col_indx(:,1):col_indx(:,2) is limited to extracting the column indices of the first row only, despite the row value being specified as ':'.
Hope this adds clarity to my question...

请先登录,再进行评论。

采纳的回答

Bruno Luong
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by