How to take elements row wise or column wise in a 2d array

A is a 5*5 array. Its needed to take 7 elements along the 1st column and 2 from the beginning of 2nd column.So the window size is 7.Now that makes it possible to take 3 windows.So 4 elements are left.this last window should be 4 leftover elements and rest zeroes.How to have a loop that gives me the 4 windows in a variable

댓글 수: 3

Without a loop
>> A=magic(5);
>> reshape( [A(:);zeros(3,1)], [7,4] )
ans =
17 6 25 16
23 12 8 22
4 18 14 3
10 1 20 9
11 7 21 0
24 13 2 0
5 19 15 0
MSP
MSP 2017년 1월 1일
편집: per isakson 2017년 1월 1일
But can u pls generalize it
In your question you say
  • "a 5*5 array" &nbsp
  • "take 7 elements [window] along" &nbsp
  • "that gives me the 4 windows in a variable" &nbsp
  • "matrix signalprocess" &nbsp
"generalize" &nbsp Yes, but in what way? Arbitrary size of the input matrix and/or the window? Along columns or rows? ...

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 31일

댓글 수: 1

window = 7;
A = randi(5,5);
n = numel(A);
left_over = mod(n, window);
if left_over == 0
stream = A(:);
else
stream = [A(:); zeros(window - left_over, 1)];
end
output = reshape(stream, window, []);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

MSP
2016년 12월 31일

댓글:

2017년 1월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by