Store data in cell arrays

조회 수: 129 (최근 30일)
Ricky
Ricky 2013년 7월 17일
Hello Everyone,
I know the question below is quite stupid but please help as I am not able o find a solution for it. This is a sample array that I have. I want to check the second column of this. The places where I get 1 should be stored together in cell array. As I start scanning the first 4 values get stored in cell array 1 then as the value in second column is 0 I do not store it . Again when I get 1 I should store but in cell array 2. The number of values that I will get is not fixed.
26.81267 1
26.82500 1
26.83733 1
26.86200 1
37.41933 0
37.40700 0
37.39466 0
37.38233 0
27.08400 1
27.07167 1
27.05933 1
27.04700 1
27.02233 1

답변 (4개)

the cyclist
the cyclist 2013년 7월 17일
편집: the cyclist 2013년 7월 17일
If A is your original array, then
C{1} = A(A(:,2)==1,:);
is a one-element cell array that stores the 9 rows with a zero.
If you only wanted to store the first column, then
C{1} = A(A(:,2)==1,1);
instead.
  댓글 수: 1
Ricky
Ricky 2013년 7월 17일
actually I want to store them in 2 cell arrays the first 4 elements go in cell array 1 and last 5 elements go in cell array 2

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


Andrei Bobrov
Andrei Bobrov 2013년 7월 17일
편집: Andrei Bobrov 2013년 7월 18일
a =[26.81267 1
26.82500 1
26.83733 1
26.86200 1
37.41933 0
37.40700 0
37.39466 0
37.38233 0
27.08400 1
27.07167 1
27.05933 1
27.04700 1
27.02233 1];
l = [true;diff(a(:,2))~=0];
l(l) = a(l,2);
ll = cumsum(l);
out = accumarray(ll(a(:,2)>0),a(a(:,2)>0,1),[],@(x){x});
OR
l = [true;diff(a(:,2))~=0];
d = diff([find(l);size(a,1)+1]);
out = mat2cell(a(a(:,2)>0,1),d(a(l,2)>0),1);

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 17일
편집: Azzi Abdelmalek 2013년 7월 17일
i2=[1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 1 1 0 1];
a=[i2(1) diff(i2)];
idx1=find(a==1);
idx2=find(a==-1);
if numel(idx2)<numel(idx1)
idx2(end+1)=numel(a)+1;
end
out=arrayfun(@(x1,x2) i2(x1:x2),idx1,idx2-1,'un',0)
  댓글 수: 5
Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 17일
It's true, I 've used arrayfun several times, even when it's not the more efficient solution (because I'm not yet good with accumarray), for 'Uniforme output', it's dictated by each case,"I am not guilty!"
Evan
Evan 2013년 7월 17일
편집: Evan 2013년 7월 17일
A good majority of my answers rely on arrayfun/cellfun as well (also regexp/regexprep). I think, for me, it's an unfortunate symptom of spending too much time playing Cody.

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


Swapnali Gujar
Swapnali Gujar 2013년 7월 17일
You can try below code. This code will give you output as a cell whose first 2 cells will have names as "cell_array1" and "cell_array2", since there are 2 times when 1s appear in your input array in continuation (i.e. first 4 times 1s and next 5 times 1s). Below each column name, you will find respective 1s of each scan.
clc
clear all;
arr = [26.81267 1;
26.82500 1;
26.83733 1;
26.86200 1;
37.41933 0;
37.40700 0;
37.39466 0;
37.38233 0;
27.08400 1;
27.07167 1;
27.05933 1;
27.04700 1;
27.02233 1;
]
size_arr = size(arr);
j=0;
scan = 0;
for i=1:length(arr)
if((arr(i,2)) == 1.000)
j=j+1;
scan=j;
if (i<length(arr)) && (arr((i+1),2) == 1.000)
j = j-1;
end
cell_arr{scan} = strcat('cell_array',num2str(scan)); %creates numbered cell arrays
end
end
%===== Now start storing the values into the cell arrays.
index=1;
columns = length(cell_arr)
k=1;
for i =1:length(arr)
if(((arr(i,2)) == 1.000)) && (k<=columns)
cell_arr(index+1,k) = num2cell(arr(i,2));
index = index+1;
elseif (arr(i,2) == 0.000)&&((arr(i+1,2)) == 1.000)
k = k+1;
index=1;
end
end
%end of file
This will give you output as cell_arr =
'cell_array1' 'cell_array2'
[ 1] [ 1]
[ 1] [ 1]
[ 1] [ 1]
[ 1] [ 1]
[] [ 1]
You may need further processing to access these columns separately as per your need.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by