How to fill just such successive elements in row of matrix?

30 visualizzazioni (ultimi 30 giorni)
Hello!
i have matrix A (733*3600) randomly generated containing such values,i have a condition , if the value of element (i,j)=k, i want to fill another matrix B starting from element(i,j) until another specific element with a condition to not exceed array bounds.
for example i have k=50
in matrix B , i will fill 500 succesive elements starting from element with value k=50 in matrix A.
for the succesive elements , i want to stop until the limit of number of columns.
i already have a code to not exceed array bounds in this link https://www.mathworks.com/matlabcentral/answers/1799345-how-to-put-condition-in-rows-and-columns-for-not-exceed-array-bounds?s_tid=srchtitle , i tried to fill elements one by one ,but it is hard to apply it now with number of columns so large.
i hope that you could help me!
thanks
  2 Commenti
dpb
dpb il 1 Ott 2022
"to fill another matrix B starting from element(i,j) until another specific element "
What about multiple locations match in a given row? Take first, last, all matches and start from there or what???
Safia
Safia il 1 Ott 2022
@dpb the based matrix contains in each row just one value, the rest all zeros. i want to fill another matrix starting by the position of this element until specific column. i posted my code in this link may could more understand.

Accedi per commentare.

Risposta accettata

Torsten
Torsten il 1 Ott 2022
Modificato: Torsten il 1 Ott 2022
A = [ 0 50 0 0 0
0 0 50 0 0
0 0 50 0 0
0 0 0 50 0];
S = linspace(1,4,4);
T = 2;
B = zeros(size(A));
for i = 1:size(A,1)
jstart = find(A(i,:)==50,1);
jend = min(jstart+numel(S)-1,size(A,2));
B(i,jstart:jend) = (50*S(1:jend-jstart+1))/T;
end
B
B = 4×5
0 25 50 75 100 0 0 25 50 75 0 0 25 50 75 0 0 0 25 50

Più risposte (2)

Walter Roberson
Walter Roberson il 30 Set 2022
Consider for example,
A(i : min(i+k-1, end), j)
This would refer to at most k consecutive rows starting at row i but would stop at the array boundary.
  1 Commento
Safia
Safia il 30 Set 2022
@Walter Roberson i will explain you the code may could more understand
i created a vector from 1 to 720 element.
S=linspace(1,720,720)
i already have a matrix A(1826*3600) randomly generated contains such values.
i wrote this code to fill another matrix P.
for i=1:1826
for j=1:3600
for k=1:720
if A(i,j)==50
P(i,j)=(50*S(k))/3600;
else P(i,j)=0;
end
end
end
end
for matrix P , i want to fill just 720 columns, starting by the one having the specific value in matrix A, and we will stop until the bounds of array.

Accedi per commentare.


Matt J
Matt J il 1 Ott 2022
Modificato: Matt J il 1 Ott 2022
Is this what you want?
A=[9, 50, 18 3;
11 7 50 10;
1 2 3 50;
50 3 4 5]
A = 4×4
9 50 18 3 11 7 50 10 1 2 3 50 50 3 4 5
B=cumsum(A==50,2)*50
B = 4×4
0 50 50 50 0 0 50 50 0 0 0 50 50 50 50 50
C=B+~B.*A
C = 4×4
9 50 50 50 11 7 50 50 1 2 3 50 50 50 50 50
  1 Commento
Safia
Safia il 1 Ott 2022
Modificato: Safia il 1 Ott 2022
@Matt J Hello!
Matrix A is already generated, in each row there is just one value and other elements are "0". for example
A [ 0 50 0 0 0
0 0 50 0 0
0 0 50 0 0
0 0 0 50 0]
Now i want to fill specific columns in matrix B. So i generated a vector contains the number of elements i want to fill. for example 4 elements
S=linspace(1,4,4)
for i=1:4
for j=1:5
for k=1:4
if A(i,j)==50
B(i,j)=(50*S(k))/T; %T is a value
else B(i,j)=0;
end
the result will be like this for T=2
S=[1 2 3 4]
B [ 0 25 50 75 100
0 0 25 50 75
0 0 25 50 75
0 0 0 25 75]
i want that elements can't exceed the bounds of matrix.

Accedi per commentare.

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by