could anyone help me how to reshape the matrix.
1 view (last 30 days)
Show older comments
jaah navi
on 11 May 2019
Commented: John D'Errico
on 12 May 2019
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 Comments
Accepted Answer
John D'Errico
on 11 May 2019
Edited: John D'Errico
on 11 May 2019
You are not asking to do a reshape, which does not change the total number of elements.
A = A(1,:);
2 Comments
John D'Errico
on 12 May 2019
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.
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!