Get range of integer values from adjacent column elements (integers) for multiple rows.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Raymond MacNeil
el 25 de Nov. de 2018
Comentada: Raymond MacNeil
el 25 de Nov. de 2018
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
Respuesta aceptada
Bruno Luong
el 25 de Nov. de 2018
Editada: Bruno Luong
el 25 de Nov. de 2018
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!