concatanate arrays upon condition

조회 수: 3 (최근 30일)
Levente Gellért
Levente Gellért 2025년 6월 20일
댓글: Walter Roberson 2025년 6월 23일
Dear MatLAb Community,
I would like to concatanate selected coloumns of arrays, some of which are empty arrays, and this is varying dataset by dataset.
How can I exclude the empty arrays from concatanation?
Let's say now , distr3 and distr6 are empty but this my vary.
distr = cat(1,distr1(:,ch1),distr2(:,ch2),distr3(:,ch3),distr4(:,ch4),distr5(:,ch5),distr6(:,ch6),distr7(:,ch7),distr8(:,ch8));
Thans for you suggestions
BEst
lg
  댓글 수: 1
Stephen23
Stephen23 2025년 6월 20일
편집: Stephen23 2025년 6월 22일
"Thans for you suggestions"
Don't store your data in lots of separate variables with numbers in their names.

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

채택된 답변

Matt J
Matt J 2025년 6월 20일
편집: Matt J 2025년 6월 20일
The way your data is held, as separate enumerated variables, doesn't leave many options. The first thing would be to undo that:
distrCell = arrayfun(@(i)eval("distr"+i),1:8,'uni',0); %should be unnecessary
chCell = arrayfun(@(i)eval("ch"+i),1:8,'uni',0); %should be unnecessary
Then, you can do,
keep=~cellfun('isempty',distrCell);
distrCell=distrCell(keep);
chCell=chCell(keep);
distr=cell2mat( cellfun(@(d,c) d(:,c), distrCell,chCell ,'uni',0 ) )
  댓글 수: 2
Levente Gellért
Levente Gellért 2025년 6월 20일
이동: Torsten 2025년 6월 20일
Dear Matt J, first I started to understand and then run your first line. Although the distr1 variable is in the workspace, the following error message comes:
Error using eval
Undefined function or variable 'distr1'.
Error in @(i)eval("distr"+i)
matlab.internal.language.introspective.errorDocCallback('@(i)eval(
Error: Character vector is not terminated properly.
Can you help further?
best
lg
Matt J
Matt J 2025년 6월 20일
편집: Matt J 2025년 6월 20일
Modify along the following lines:
distr1=17; distr2=5; distr3=68;
distrCell = arrayfun(@(i)evalin('caller',"distr"+i),1:3,'uni',0) %should be unnecessary
distrCell = 1×3 cell array
{[17]} {[5]} {[68]}
Or, even better, recreate the data from scratch, but instead of distr1, distr2,... store the arrays in a distrCell variable directly.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2025년 6월 20일
At least in R2025a, there is no problem in using "," to concatenate empty datasets, as long as they are empty along either the first or second dimension. (There can be problems if they are empty along the third dimension and the sizes of the first and second dimension do not match the existing data.)
A = zeros(5,3);
B = ones(5,0);
C = zeros(5,2);
D = []; size(D)
ans = 1×2
0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
E = ones(3,0);
F = ones(0,3);
G = ones(10,9,0);
[A,B,C]
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[A,D,C]
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[A,E,C]
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[A,F,C]
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[A,G,C]
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
  댓글 수: 4
Walter Roberson
Walter Roberson 2025년 6월 23일
편집: Walter Roberson 2025년 6월 23일
Is the error you recieved due to the fact that you are subscripting an empty array at columns that do not exist?
T1 = randi(9,5,3)
T1 = 5×3
2 2 1 9 8 3 9 1 5 6 5 1 5 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T2 = randi(9,7,4)
T2 = 7×4
5 7 6 3 6 9 9 3 2 3 1 3 1 6 7 5 9 4 2 3 8 3 8 7 1 1 2 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T3 = randi(9,4,1)
T3 = 4×1
2 2 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T4 = randi(9,6,3)
T4 = 6×3
3 2 8 2 9 7 2 4 7 6 1 5 3 7 7 1 8 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = @(A,c) A(:,c:c.*(c<=size(A,2)));
%use the auxillary function S to select columns
[S(T1,2); S(T2,2); S(T3,2); S(T4,2)]
ans = 18×1
2 8 1 5 4 7 9 3 6 4 3 1 2 9 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%whereas directly indexing might fail
[T1(:,2); T2(:,2); T3(:,2); T4(:,2)]
Index in position 2 exceeds array bounds. Index must not exceed 1.
Walter Roberson
Walter Roberson 2025년 6월 23일
Huh, there is a difference between cat(1,A,B) and [A;B] ...
A = zeros(5,1);
B = ones(3,0);
[A;B]
ans = 5×1
0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
cat(1, A, B)
Error using cat
Dimensions of arrays being concatenated are not consistent.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by