How to separate cells?
Mostra commenti meno recenti
I have a 4x1 cell named D that consists of:
D{1,1} = 712x10 int32
D{2,1} = 712x10 int32
D{3,1} = 866x10 int32
D{4,1} = 854x10 int32
Is there anyway to separate cells into individual matrices? I want to manipulate columns 8 and 9 of the individual matrices but when i try to use cell2mat it merges all the cells together into one large matrix. Is there a way to do it for any number of cells? For example if there were 100 instead of 4? Here is the code i have so far:
function [D,StepInfo] = ImportSpecialTextFile(filename)
fidi = fopen(filename,'rt');
k1 = 1;
while ~feof(fidi)
for k2 = 1:3
StepInfo{k1,k2} = fgetl(fidi);
end
C = textscan(fidi, ['R' repmat('%d',1,10)], 'Delimiter',',', 'CollectOutput',true);
M = cell2mat(C);
if isempty(M)
break
end
D{k1,:} = M;
Dd{k1,:} = cell2mat(D(k1));
fseek(fidi, 0, 0);
k1 = k1 + 1;
end
fclose(fidi);
end
With a call to the function:
filename = 'extractcdb.txt';
[D,StepInfo] = ImportSpecialTextFile(filename)
8 Commenti
"Is there anyway to separate cells into individual matrices?"
Sure, that is easy:
W = D{1,1};
X = D{2,1};
Y = D{3,1};
Z = D{4,1};
or
[W,X,Y,Z] = deal(D{1:4,1});
"I want to manipulate columns 8 and 9 of the individual matrices..."
That is easy, just use a loop and indexing. Indexing is simple and very efficient.
"...but when i try to use cell2mat it merges all the cells together into one large matrix"
Then don't use cell2mat! Just use indexing to access the cell array contents:
"Is there a way to do it for any number of cells? For example if there were 100 instead of 4?"
Use indexing.
There are certainly ways to create individual variables, but they are not recommended. Doing so requires dynamically naming variables, which is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, code which is hard to debug. Read this to know why:
Your cell array is perfect, there is no reason for you to pointlessly split up that cell array into lots of little arrays that would just force you into writing bad code. You should use indexing. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
Bradley Sprinkle
il 28 Giu 2019
"Is there a way to efficiently do that?"
Yes, using indexing is efficient.
Fundamentally the approach of dynamically defining variables is NOT efficient.
Indexing is efficient. What you are trying to do is not efficient, as well as being more complex, difficult to read and maintain, and harder to debug. That is why I advised you to use indexing.
"...but the code should be able to work for larger data files."
Sure, so use indexing. What is stopping you?
Bradley Sprinkle
il 28 Giu 2019
"Will indexing work for more than 4 sections of R lines?"
Of course, why not? Just define the loop to iterate over as many sections as D has:
for k = 1:numel(D)
D{k} % do whatever you want with that matrix
end
I just tried it on your example imported data:
>> for k = 1:numel(D), mean(D{k}), end
ans =
356.5 0 1 1 2.1398e+05 34.98 15256 19614 18528 45864
ans =
356.5 0 0.91573 0.9059 2.1393e+05 34.98 15302 14728 13908 45840
ans =
433.5 0 0.99885 0.99885 2.5979e+05 34.993 264.87 17045 16074 45752
ans =
427.5 0 0.99883 0.99883 2.5641e+05 37.991 345.33 17179 17216 49965
There is no restriction on the number of "sections", or how many rows they should have...
Bradley Sprinkle
il 28 Giu 2019
Stephen23
il 28 Giu 2019
"Is there a way to only manipulate the 8th and 9th columns..."
Sure, just use indexing, e.g. D{k}(:,[8,9])
Or using your example imported data:
>> for k = 1:numel(D), mean(D{k}(:,[8,9])), end
ans =
19614 18528
ans =
14728 13908
ans =
17045 16074
ans =
17179 17216
"... and hide the others?"
I have no idea what it means to "hide" a column of a matrix.
Bradley Sprinkle
il 28 Giu 2019
Risposte (1)
Star Strider
il 28 Giu 2019
This looks familiar!
‘I want to manipulate columns 8 and 9 of the individual matrices ...’
To do that for ‘D{1}’:
D1_Cols89 = D{1}(:,[8 9]);
Thjis creates an (Nx2) array. Call it whatever you wish.
You can put those into a loop as well, and do whatever you want iteratively. For example to calculate the dot product of columns 8 and 9 for each ‘D’:
for k = 1:numel(D)
DotCols(k,:) = dot(D{k}(:,8), D{k}(:,9)); % Dot Product Of Columns 8 & 9
end
If you expect vector outputs, and therefore different size results of the calculation, use cell indexing for the result (using {k} instead of (k)) instead.
4 Commenti
Bradley Sprinkle
il 28 Giu 2019
Modificato: Bradley Sprinkle
il 28 Giu 2019
Star Strider
il 28 Giu 2019
Please describe what you want to do.
If you want to calculate the differences between Columns 8 and 9, just subtract them:
B = (D{k}(:,8) - D{k}(:,9)) * 1.2;
That will keep the row size consistent wit the original row sizes, as well. (The output of the diff call will be one element shorter than the size of its argument, in whatever dimension you select.)
What do you want ‘C’ to be?
Bradley Sprinkle
il 28 Giu 2019
Star Strider
il 28 Giu 2019
I still have no idea what you want. I specifically do not understand using diff and then using cumsum.
This will at least run without error:
for k = 1:numel(D)
B = [diff(D{k}(:,[8,9]))] * 1.2;
B =[0 0;B];
C = cumsum(B) + (D{k}(:,[8,9]))
end
If you just want to add ‘B’ to columns 8 & 9, this works:
for k = 1:1%numel(D)
B = [diff(D{k}(:,[8,9]))] * 1.2;
B =[0 0;B];
C = B + (D{k}(:,[8,9]));
end
Categorie
Scopri di più su Time Series Events in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!