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

13 次查看(过去 30 天)
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 个评论
dpb
dpb 2022-10-1
"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 2022-10-1
@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.

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2022-10-1
编辑:Torsten 2022-10-1
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

更多回答(2 个)

Walter Roberson
Walter Roberson 2022-9-30
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 个评论
Safia
Safia 2022-9-30
@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.

请先登录,再进行评论。


Matt J
Matt J 2022-10-1
编辑:Matt J 2022-10-1
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 个评论
Safia
Safia 2022-10-1
编辑:Safia 2022-10-1
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by