필터 지우기
필터 지우기

could anyone help me how to reshape the matrix.

조회 수: 1 (최근 30일)
jaah navi
jaah navi 2019년 5월 11일
댓글: John D'Errico 2019년 5월 12일
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.

채택된 답변

John D'Errico
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
jaah navi
jaah navi 2019년 5월 12일
I tried with the following code:
B=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
A=[0.6399 0.8965]
if size(A)>size(B)
A=A(size(B,1),:)
social=B-A
else
[jj,kk]=size(A)
[jjj,kkk]=size(B)
zz=zeros(jjj,kkk)
zz(1:jj,1:kk)=A
social=B-zz
end
when i run the code it executes.
But when i run the code by changing A to B and B to A i am getting error for the following code:
A=[ 0.7684 0.0120;
0.5333 0.9666;
0.0684 0.8322;
0.1996 0.4453]
B=[0.6399 0.8965]
if size(A)>size(B)
A=A(size(B,1),:)
social=B-A
else
[jj,kk]=size(A)
[jjj,kkk]=size(B)
zz=zeros(jjj,kkk)
zz(1:jj,1:kk)=A
social=B-zz
end
Could you please help me to solve the issue.I want to form the code in such a way the code needs to be executed when size(A) > size(B) and when size(B)> size(A)
John D'Errico
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 CenterFile 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!

Translated by