Hello friends,
I have two cell arrays which I intend to eventually subtract from one another.
However, I have discovered they are of the same size but not same length (both attached). The cell arrays are both of size 181x1 with nested values of 44x95(double) except for the last two in the other file ('typevector_filtered').
I want to pad the empty values (in 'typevector_filtered') with zeros (0s) so that this cell array is the exact same size as the other one ('velocity_magnitude').
Any help kindly?
My attempts using this code (below) do not yield the same size (see image attached, last two rows end up being 95x1 double instead of 44x95double):
M = max(cellfun(@length, typevector_filtered));
>> Wanted = cellfun(@(x) [x; zeros(M - numel(x), 1)], typevector_filtered, 'un', 0);
Thank you.

 採用された回答

KSSV
KSSV 2021 年 4 月 5 日
編集済み: KSSV 2021 年 4 月 5 日

0 投票

load typevector_filtered.mat
S = size(typevector_filtered{1}) ;
for i = 1:length(typevector_filtered)
if ~isequal(size(typevector_filtered{i}),S)
typevector_filtered{i} = zeros(S) ;
end
end
Or, using Cellfun (which also uses loop inside)
load typevector_filtered.mat
S = size(typevector_filtered{1}) ;
P = cell2mat(cellfun(@size,typevector_filtered,'UniformOutput',false)) ;
[c,ia] = ismember(P,S,'rows') ;
idx = find(c==0) ;
typevector_filtered(idx) = {zeros(S)} ;

5 件のコメント

Maskus Kit
Maskus Kit 2021 年 4 月 5 日
Just perfect, thank you ever so much! That works.
Quick one (this was in the first part of my question so I don't think it deserves a totally new question, and might also be something rather 'easy'). How do I go about subtracting these two cell arrays? (typevector_filtered - velocity_magnitude)? Thank you so so much :)
KSSV
KSSV 2021 年 4 月 5 日
Run a loop and subtract; save the result in another cell.
Maskus Kit
Maskus Kit 2021 年 4 月 5 日
To be completeley honest I have no idea on how to go about that, do you mind just giving me a hypothetical code or should I just write that out as a different question?
Thanks very much.
KSSV
KSSV 2021 年 4 月 5 日
N = length(typevector_filtered) ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = typevector_filtered{i} - velocity_magnitude{i} ;
end
Maskus Kit
Maskus Kit 2021 年 4 月 5 日
Just perfect, you're a life saver! If I could get you a covid jab right now I would immedaitely, haha.
Thanks so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2021 年 4 月 5 日

コメント済み:

2021 年 4 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by