Hi
I have 100 arrays i.e. each have one row and diffrent columns (say size is A = 1 * 167 and B = 1 * 189 and so on). I want to make one matrix in way that the first row is A, second row is B and so on... But the columns will remain different. How can I do it?

댓글 수: 4

Benjamin Thompson
Benjamin Thompson 2024년 1월 28일
Like this?
>> A = [1 2 3 4]
A =
1 2 3 4
>> B = [5 6 7 8]
B =
5 6 7 8
>> C = [A; B]
C =
1 2 3 4
5 6 7 8
If this is not right please explain more what output you want. Perhaps with a smaller example of 4 arrays what will the output look like?
No, like this:
A = [1 2 3 4]
A = 1×4
1 2 3 4
B = [5 6 7 ]
B = 1×3
5 6 7
C = [A; B]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Ahmed
Ahmed 2024년 1월 29일
@Torsten No this is not working e.g.,
ITER = [iters_1; iters_2];
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in PLOTTING_UNCERTINITY (line 172)
ITER = [iters_1; iters_2];

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

 채택된 답변

Torsten
Torsten 2024년 1월 28일
이동: Torsten 2024년 1월 28일

1 개 추천

Either use a cell array or dimension the matrix to have the maximum number of columns of the 100 row vectors:
A = [1 23 45];
B = [1 2];
M1 = cell(2,1);
M1{1} = A;
M1{2} = B;
M1
M1 = 2×1 cell array
{[1 23 45]} {[ 1 2]}
n = max(size(A,2),size(B,2));
M2 = zeros(2,n);
M2(1,:) = [A,zeros(1,n-size(A,2))];
M2(2,:) = [B,zeros(1,n-size(B,2))];
M2
M2 = 2×3
1 23 45 1 2 0

댓글 수: 5

Ahmed
Ahmed 2024년 1월 29일
Thanks, it is working but if I have 500 rows then it will be too long to code this way. Is there any short way?
Walter Roberson
Walter Roberson 2024년 1월 29일
I have 100 arrays
What form do you have the 100 arrays in?
If you have them stored in seperate variables... then you have a problem. Don't store them in seperate variables.
Ahmed
Ahmed 2024년 1월 29일
Hi,
Thanks for your reply. actually all the variables are stored separetly. If I do as @Torsten sugested (e.g., given below), zero appears to make size equal. How to get rid off these zeros during plotting? You can see these are appearing in figure as well (circled part).
hist_1 = hist_1./max(hist_1);
hist_2 = hist_2./max(hist_2);
M1 = cell(2,1);
M1{1} = hist_1;
M1{2} = hist_2;
n = max(size(hist_1,2),size(hist_2,2));
M2 = zeros(2,n);
M2(1,:) = [hist_1,zeros(1,n-size(hist_1,2))];
M2(2,:) = [hist_2,zeros(1,n-size(hist_2,2))];
figure, plot(iters_2,M2)
Stephen23
Stephen23 2024년 1월 29일
편집: Stephen23 2024년 1월 29일
"How to get rid off these zeros during plotting?"
Replace ZEROS with NAN. Or use the approach I gave you here:
"actually all the variables are stored separetly."
Having lots of separate variables makes your data much harder to work with.
It is much better to use a cell array or structure array.
Walter Roberson
Walter Roberson 2024년 1월 29일
Whatever process is loading all that data into seperate variables: it would be better if it loaded it all into a cell or a struct array to start with.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2024년 1월 28일

댓글:

2024년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by