running matrix in a loop to produce another matrix

조회 수: 1 (최근 30일)
JL
JL 2019년 8월 22일
댓글: JL 2019년 8월 22일
Hi everyone, I have z
z = [1 1 0 0;
1 0 1 0;
1 0 0 1;
0 1 1 0;
0 1 0 1;
0 0 1 1;]
each of z's row are states that I want my code to go through one by one (from 1 1 0 0 to 0 0 1 1) , and after each state is run, it produces w. after running all the 6 states from z,
w = [a;
a;
a;
a;
a;
a;];
Is there a code where I can run each state of z in a loop, and produces w?
  댓글 수: 2
Ted Shultz
Ted Shultz 2019년 8월 22일
what is "a"?
JL
JL 2019년 8월 22일
"a" is calculated number. Meaning, for each time it loop, "a" is generated until the loop finishes.

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

채택된 답변

Ted Shultz
Ted Shultz 2019년 8월 22일
This code will loop over each row of Z and put the results into w. I'm not sure what you want to do with each row.
z = [1 1 0 0;
1 0 1 0;
1 0 0 1;
0 1 1 0;
0 1 0 1;
0 0 1 1;]
numRows = size(z,1); % counts the number of rows
w = nan(numRows,1); % preallocate w;
for ii = 1:numRows
thisRow = z(ii,:);
% do something here to get a?
a=sum(thisRow);
w(ii) = a;
end
  댓글 수: 3
JL
JL 2019년 8월 22일
Yes, I put it up as anothe question

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by