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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Chaoyang Jiang
Chaoyang Jiang il 4 Mag 2018
Modificato: Chaoyang Jiang il 4 Mag 2018
@Guillaume. I have revised my code and post the expected results in my question.
Chaoyang Jiang
Chaoyang Jiang il 4 Mag 2018
Modificato: Chaoyang Jiang il 4 Mag 2018
@Jan, it should work now. I have revised my code and post the expected results in my question.

Accedi per commentare.

Risposte (1)

Guillaume
Guillaume il 3 Mag 2018
Modificato: Guillaume il 4 Mag 2018
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 Commenti
Chaoyang Jiang
Chaoyang Jiang il 4 Mag 2018
Modificato: Chaoyang Jiang il 4 Mag 2018
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 il 4 Mag 2018
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

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by