putting the answer in a matrices

조회 수: 6 (최근 30일)
fyza affandi
fyza affandi 2019년 2월 25일
편집: Jan 2019년 2월 25일
I have matrix a. Then, I find which column in each row has number bigger than 1. The code is as below
a =
5 0 0 0 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0
>> for cpart=1:size(a,1)
b=a(cpart,:);
row = find(b ~=0)
end
row =
1
row =
1 2
row =
1 2
row =
1 3
How can I put the row in a matrices ?(as shown below)
row = [1 0
1 2
1 2
1 3]
  댓글 수: 1
Image Analyst
Image Analyst 2019년 2월 25일
What are you going to do if you have row? Because I'm thinking you don't even need it and whatever you're going to do can be done much easier with a mask
mask = a~=0; % Create a logical 2-D matrix.
So I don't want to tell you how to get row if it just makes things more complicated for what you eventually want to do. So, what will you do with row if you had it?

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

채택된 답변

Jan
Jan 2019년 2월 25일
편집: Jan 2019년 2월 25일
With a loop:
nA = (a ~= 0);
nRow = size(a, 1);
nCol = max(sum(nA, 2));
result = zeros(nRow, nCol); % Pre-allocation
for c = 1:nRow
v = find(nA(c, :));
result(c, 1:numel(v)) = v;
end

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 2월 25일
b=arrayfun(@(x)find(a(x,:)),1:size(a,1),'un',0);
M=max(cellfun('prodofsize',b));
C=cellfun(@(x)[x zeros(1,M-numel(x))],b,'un',0);
Result=vertcat(C{:})
  댓글 수: 1
madhan ravi
madhan ravi 2019년 2월 25일
To remove rows with all zeros:
Result(sum(Result,2)~=0,:)

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by