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 Comments

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.

Sign in to comment.

Answers (1)

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 Comments

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

Sign in to comment.

Categories

Tags

Asked:

on 3 May 2018

Edited:

on 4 May 2018

Community Treasure Hunt

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

Start Hunting!