How to resign each non-zero column elements of a matrix according to a vector

조회 수: 1 (최근 30일)
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 3일
편집: Guillaume 2018년 5월 4일
How to resign each non-zero column elements of a maxtrix a according to the vector b1. E.g., for a(:,1), non-zero elements is [2,1,3,7]. b1(:,1)=[2;1] defines the first two non-zero elements, and third non-zero element of a(:,1).
As the size of c is changing, it seems I have to use cell here. However, cell cannot be vectorized and this code is so slow. May I know if there other smart ideas? Thank you.
a=[2 1 1;
1 0 2;
3 8 0;
7 1 8
0 1 7];
b1=[2 1 1;
1 2 3];
c=cell(3,2);
for i=1:size(a,2)
temp{i,1}=a(a(:,i)>0,i);
for j=1:size(b1,1)
c{i,j}=temp{i,1}(1:b1(j,i));
temp{i,1}(1:b1(j,i))=[];
end
end
For
i=1,temp{i,1}=[2,1,3,7];
j=1; c{1,1}=[2,1];% temp{i,1}(1:2)
temp{i,1}=[3,7]; %temp{i,1}(1:2)=[];
j=2; c{1,2}=[3]; %temp{i,1}(1:1)
temp{i,1}=[7]; %temp{i,1}(1:1)=[];
The results should be c{1,1}=[2,1];c{1,2}=[3]; c{2,1}=[1];c{2,2}=[8,1]; c{3,1}=[1];c{3,2}=[2,8,7];
  댓글 수: 4
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 4일
편집: Chaoyang Jiang 2018년 5월 4일
@Guillaume. I have revised my code and post the expected results in my question.
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 4일
편집: Chaoyang Jiang 2018년 5월 4일
@Jan, it should work now. I have revised my code and post the expected results in my question.

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

답변 (1개)

Guillaume
Guillaume 2018년 5월 3일
편집: Guillaume 2018년 5월 4일
Well, yes if you want to store column vectors of different length, you don't have a choice but to use cell arrays. The question is thus: do you actually need to store column vectors of different length? What are you going to do with these? If you can avoid that step you'll be better off
With regards to your code,
  • 2D indexing is marginally slower than linear indexing. You don't need 2D indexing in your code.
  • There is no reason for temp to be a cell array
Plus you've made some mistake (always extracting the first column and only getting one element from each column, incorrect syntax for cell, etc.
a=[2 1 1;
1 0 2;
3 8 0;
7 1 8
0 1 7];
b1=[2,1,1];
c = cell(1, size(a, 2));
for col = 1:size(a, 2)
temp = a(logical(a(:, col)), col);
c{col} = a(1:b1(col));
end
  댓글 수: 2
Chaoyang Jiang
Chaoyang Jiang 2018년 5월 4일
편집: Chaoyang Jiang 2018년 5월 4일
Thank you for your answer. For the generated cell c, I wanna read the random numbers from each cell of c, i.e., c{1,1}. I have also updated my questions.
Guillaume
Guillaume 2018년 5월 4일
I'm assuming that c should be 2x3 (== size of b1) and not 3x2 as you've written and I'm assuming that the lonely 3 should be a 1. Otherwise, I completely misunderstood what you want.
a=[2 1 1;
1 0 2;
3 8 0;
7 1 8
0 1 7];
b1=[2 1 1;
1 2 3];
c = cell(size(b1));
for col = 1:size(b1, 2)
for row = 1:size(b1, 1)
temp = a(logical(a(:, col)), col);
c{row, col} = temp(1:b1(row, col));
end
end

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

카테고리

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