필터 지우기
필터 지우기

find 1s in the Matrix

조회 수: 4 (최근 30일)
Matin jaberi
Matin jaberi 2022년 9월 29일
답변: Matin jaberi 2022년 9월 30일
Hi All,
I have a matrix 31996x66 and i need to write a if/else and for loop to do following.
the for loop must got through column 48 to 65 and anywhere the number is 1 it goes 36 cells back ()to the same row and save the number and if zero doesnt do anything.
for example:
if in row 500 coulumn 50 number is 1 then (50-36=14) it should go to row 500 column 14 and take the number and save in new table.
At the end the new Matrix should be 31996x(number of values for 1 in each row.)
  댓글 수: 6
Matin jaberi
Matin jaberi 2022년 9월 29일
It has got nothing to do with my clients requirements. its me wanting to automate the excel sheet so the matlab run and find the numbers for me rather than I go through excel sheet myself. And there is no othere structure.
Matin jaberi
Matin jaberi 2022년 9월 29일
there might be many different ways to find the numbers I need but thats the simplest script i came up with to get what I need.

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 29일
firstcol = 48;
lastcol = 65;
offsetcol = 14;
idx = firstcol:lastcol;
offsetidx = idx-offsetcol;
result = YourMatrix(:,offsetidx) .* (YourMatrix(:,idx) == 1);
at the end the new Matrix should be 31996x(number of values for 1 in each row.)
You cannot do that; there could be a different number of 1's in each row and numeric matrices cannot have a different number of columns in each row.
The above code outputs an array the size of the subset, with 0 for the entries where the condition was not met.
I notice, by the way, that 62-14 = 48, so columns 48, 49, 50, 51 both act as numeric sources (values to be fetched) and as control information about which values are to be fetched. Is that overlap desired?
  댓글 수: 2
Matin jaberi
Matin jaberi 2022년 9월 29일
Thanks for your answer, the offset call is 36 not 14. As i mentioned before if column number is 50 then take 36 out and the desired col would be col 14.
Walter Roberson
Walter Roberson 2022년 9월 29일
firstcol = 48;
lastcol = 65;
offsetcol = 36;
idx = firstcol:lastcol;
offsetidx = idx-offsetcol;
result = YourMatrix(:,offsetidx) .* (YourMatrix(:,idx) == 1);

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

추가 답변 (1개)

Matin jaberi
Matin jaberi 2022년 9월 30일
so the box highlighted yellow is col 48 where number ==1 and the green one is column 14 is the same row. this should apply only when number is ==1

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by