could anyone help me how to reshape the matrix.
1 回表示 (過去 30 日間)
古いコメントを表示
I am having two matrices A=[3x2] and B=[1x2]
i want to reshape the size of A such that it should be equal to B,which mean A should contain only the first row.
could anyone please help me on this.
0 件のコメント
採用された回答
John D'Errico
2019 年 5 月 11 日
編集済み: John D'Errico
2019 年 5 月 11 日
You are not asking to do a reshape, which does not change the total number of elements.
A = A(1,:);
2 件のコメント
John D'Errico
2019 年 5 月 12 日
Your problem is that you do not understand how if statements work in MATLAB, or you do not understand what size returns.
B=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
A=[0.6399 0.8965]
Now, what are size(A) and size(B)?
size(A)
ans =
1 2
size(B)
ans =
4 2
Now, what happens when you use if?
help if
if Conditionally execute statements.
The general form of the if statement is
if expression
statements
ELSEIF expression
statements
ELSE
statements
END
The statements are executed if the real part of the expression
has all non-zero elements.
So, what hapens when you try the statement:
if size(A) > size(B)
What does that comparison return?
size(A) > size(B)
ans =
1×2 logical array
0 0
Does the REAL part of that result have ALL non-zero elements?
Instead, you might want to use the second argument to size. For example, do a comparison like this:
if size(B,1) > size(A,1)
disp('IT WORKS!')
else
disp('The sky is falling.')
end
IT WORKS!
So when B had more rows in it than A, the if branch executes. See what happens when we change the if clause to not use the second argument to size, even though we know that B indeed is a "larger" array than A, that is, it has more rows than A.
if size(B) > size(A)
disp('IT WORKS!')
else
disp('The sky is falling.')
end
The sky is falling.
So the primary branch in the if failed to execute, dropping down into the else clause.
The point is, you need to understand how the if statement works. You need to understand size.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!