Generate new matrix from old matrix with specific conditions
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ali Kareem
el 13 de Mayo de 2016
Comentada: Roger Stafford
el 13 de Mayo de 2016
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
0 comentarios
Respuesta aceptada
Roger Stafford
el 13 de Mayo de 2016
[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 comentarios
Roger Stafford
el 13 de Mayo de 2016
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
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!