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

조회 수: 8 (최근 30일)
Safia
Safia 2022년 9월 30일
댓글: Safia 2022년 10월 1일
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
  댓글 수: 1
Safia
Safia 2022년 10월 1일
@Torsten it works well! thank you very much! you always help me with efficient solution.

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

추가 답변 (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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by