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

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

Now I don't understand at all what the elements of b1 mean. Can you show what c should be in your example?
The code is strange. Why do you set temp{i,1} repeatedly to [], although it is overwritten in the next iteration again? c=cell{3,2} is an error, perhaps you mean c=cell(3,2) with round parentheses. b1(i) looks like a typo, because i depends on the size of a. The code fails in the first iteration, because temp is a nested cell: {{4 x 1 double}}. b(i) is 2, but temp is a scalar cell.
I do not understand the explanations, what the code should do and the posted code does not work. What is the wanted output for the given example data a and b1?
@Guillaume. I have revised my code and post the expected results in my question.
@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

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.
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

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

카테고리

도움말 센터File Exchange에서 System Commands에 대해 자세히 알아보기

태그

질문:

2018년 5월 3일

편집:

2018년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by