Hello!
I have a problem in my code, can anyone help me please?
i have a matrix A(1826*3600) randomly generated contains such values.
i will fill another matrix B based on elements of A, but not all elements(such specific columns)
the first step i did is to generate a vector contains number of specific columns i want to fill.
this is my case :
i created a vector from 1 to 720 element(number of specific columns i want to fill).
S=linspace(1,720,720)
i will check the value in matrix A (in this case for value =50), then i will fill P with my formula.
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
this code doesn't work, also i need to elements can't exceed the bounds of my matrix.
i will be grateful if you could help me!
Please HELP!!!!

댓글 수: 5

Safia
Safia 2022년 10월 1일
편집: Safia 2022년 10월 1일
@Walter Roberson did you write somthing?
Hi Safia,
you can probably do this with fewer do loops, but the first thing is to get the code to do what you intend. Inside the innermost for loop, k = 1:720, you are doing the same if check every time through. That's because the if check depends on i and j, but not k. Then if the if check passes, with the line
P(i,j)=(50*S(k))/3600
you are writing into the P(i,j) element 720 times, so P(i,j) will end up having only the last value involving S(720). The other 719 values are overwritten. So you need to determine what is supposed to happen.
Also, unless the A(i,j) data is sure to be integers, the if check with A(i,j)==50 may fail, for example if A(i.j) = 50+1e-15 or some such.
Walter Roberson
Walter Roberson 2022년 10월 1일
Safia: no, I reformatted your code
Safia
Safia 2022년 10월 1일
@David Goodmanson Hi! thank you for your reply! i want to fill 720 columns in matrix B , that's why i put the vector S. i'm trying to find easier solution than fill each column with loop if.
for k=1:720
if A(i,j)==50
Does A(i, j) change inside the for k loop? The next k value, would you not be testing the same A(i, j)? So the end result is going to be the same as it would be if you only did for k=720 and no other iterations.

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

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 10월 1일

0 개 추천

Here is the corrected code:
A=randi([0, 111], 1826, 3600);
S=1:720;
P= zeros(1826, 3600);
for i=1:1826
for j=1:3600
for k=1:720
if A(i,j)==50
P(i,j)=(50*S(k))/3600;
end
end
end
end

댓글 수: 1

Safia
Safia 2022년 10월 1일
@Sulaymon Eshkabilov thank you for you reply! but it displays the same result as the code i posted above

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

카테고리

도움말 센터File Exchange에서 Genomics and Next Generation Sequencing에 대해 자세히 알아보기

태그

질문:

2022년 9월 30일

댓글:

2022년 10월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by