For loop with if statement!

조회 수: 1 (최근 30일)
Sophia
Sophia 2019년 9월 27일
답변: the cyclist 2019년 9월 27일
Here is the samplf of what i am trying to do-
clear all;
clc;
s = [1 2 3; 4 0.5 1; 1.3 2 1; 1.5 1.6 2.8]
a(1,:,:) = [ 1 2 3 ; 4 5 6; 7 8 9;10 11 12]
a(2,:,:) = [ 11 12 13 ; 14 15 16; 17 18 19;110 111 112]
a(3,:,:) = [ 111 112 113 ; 114 115 116; 117 118 119;1110 1111 1112]
dd= []
for i = 1:size(a,1)
for j = 1:size(a,2)
for k = 1:size(a,3)
if s(j,k)>1.5
dd(:,:) = double(a(:,j,k));
else
end
end
end
end
%Expected Answer dd = [2 3 4 8 11 12; 12 13 14 18 111 112;112 113 114 118 1111 1112];
Can anyone tell me how to get the expected answer, i created this sample to understand the problem with some simplicity.
  댓글 수: 1
Ahmed Zankoor
Ahmed Zankoor 2019년 9월 27일
Firstly, you get an assignment error for matrix a. But it works if you initialize the matrix
a = zeros(3,4,3);
a(1,:,:) = [ 1 2 3 ; 4 5 6; 7 8 9;10 11 12]
a(2,:,:) = [ 11 12 13 ; 14 15 16; 17 18 19;110 111 112]
a(3,:,:) = [ 111 112 113 ; 114 115 116; 117 118 119;1110 1111 1112]
The second part is not clear at all.
  • a(:,j,k) is a 3*1 vector, so you can not form dd like your expected answer.
  • dd should be initialized so that you can assign different values at differnt positions.

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

답변 (1개)

the cyclist
the cyclist 2019년 9월 27일
It does not give the expected answer, but using
dd = [dd double(a(:,j,k))];
instead of
dd(:,:) = double(a(:,j,k));
is a step toward what you are expecting, and maybe is enough for you to figure it out?
Also, MATLAB numeric variables are type double by default, so you could just do
dd = [dd a(:,j,k)];
to get the same result.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by