필터 지우기
필터 지우기

Generate new matrix from old matrix with specific conditions

조회 수: 1 (최근 30일)
Ali Kareem
Ali Kareem 2016년 5월 13일
댓글: Roger Stafford 2016년 5월 13일
Hello,
I have matrix R(100*100) and I want to generate new matrix A(100*100) as below conditions
If I have a frequency to add a new condition(let us say frequency = 20). This mean I will have five conditions inside my domain `(100/20)=5
From 1 to 20, from 21 to 40, from 41 to 60, from 61 to 80, and from 81 to 100.
Let us assume my original matrix is
[1 1 1……..1]
[2 2 2 …….2]
[3 3 3 ………3]
And so on
[100 100 100 ..100]
The new matrix must be
From 1 to 20 same as old matrix
[1 1 1……..1]
[2 2 2 …….2]
[3 3 3………3]
From 21 to 40
A(21,j)=R(21,j)+R(1,j) (row 21 + row 1)
A(22,j)=R(22,j)+R(2,j) (row 22 + row 2)
A(23,j)=R(23,j)+R(3,j) (row 23 + row 3)
And so on
From 41 to 60
A(41,j)=R(41,j)+R(21,j) +R(1,j) (row 41+row 21 + row 1)
A(42,j)=R(42,j)+R(22,j) +R(2,j) (row 42+row 22 + row 2)
And so on
A(81,j)=R(81,j)+R(61,j)+R(41,j)+R(21,j)+R(1,j) (row 81+row 61 + row 41 + row 21+ row 1)
At each time that I reach to the frequency, I will have a new condition. My question is there any sufficient way to do that? I wrote my code and its work fine but each time I change frequency I will have a new condition. For the case above I have 5 conditions but if I use frequency 5 I will have 20 conditions and I need to change all equations. I mean I need code to be able to handle any frequency
I wrote below code
clc;
clear;
prompt = 'Enter Frequency='; %Frequency=20
N= input(prompt);
Frequency=N;
one_step=1/Frequency; %Frequency
time=Frequency*one_step;
number_of_steps=time/one_step; %Number of steps until next condition
total_steps=100;
R1 = rand(100,100);
Number_of_Lines=(total_steps/number_of_steps)+1; %100/20
A(100,100)=0;
for i=1:100
for j=1:100
if i>=1 && i<=number_of_steps
A(i,j)=R1(i,j);
elseif i>number_of_steps && i<= 2*number_of_steps
A(i,j)=R1(i,j)+R1(i-number_of_steps,j);
elseif i>2*number_of_steps && i<= 3*number_of_steps
A(i,j)=R1(i,j)+R1(i-number_of_steps,j)+R1(i-2*number_of_steps,j);
elseif i>3*number_of_steps && i<= 4*number_of_steps
A(i,j)=R1(i,j)+R1(i-number_of_steps,j)+R1(i-2*number_of_steps,j)...
+R1(i-3*number_of_steps,j);
elseif i>4*number_of_steps && i< 5*number_of_steps
A(i,j)=R1(i,j)+R1(i-number_of_steps,j)+R1(i-2*number_of_steps,j)...
+R1(i-3*number_of_steps,j)+R1(i-4*number_of_steps,j);
end
end
end

채택된 답변

Roger Stafford
Roger Stafford 2016년 5월 13일
[m,n] = size(R);
f = 20; % <-- Check that f is a divisor of m
A = reshape(cumsum(reshape(R,f,m/f,n),2),m,n);
  댓글 수: 2
Ali Kareem
Ali Kareem 2016년 5월 13일
Hello,
Thank you so much for your reply. Please, I just have one problem. if the division of (m/f) is not an integer. let us assume f=15. How I can handle this situation?
Roger Stafford
Roger Stafford 2016년 5월 13일
One way would be to do this:
c = ceil(m/f);
R2 = [R;zeros(f*c-m,n)]; % Expand R to multiple of f rows
A = reshape(cumsum(reshape(R2,f,c,n),2),f*c,n);
A = A(1:m,:); % Remove those extra rows

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by