If statement in a for loop

2 Ansichten (letzte 30 Tage)
MarshallSc
MarshallSc am 27 Aug. 2021
Beantwortet: Wan Ji am 27 Aug. 2021
I have a 4*4 array, that each contains a 10*10 matrix. I want to assign a value to the components that are bigger than 1. I wrote the code below but the array still gives me the old answer. I my code correct?
for i=1:4
for j=1:4
for k=1:10
for r=1:10
if A{i,j}(k,r)> 1
A{i,j}(k,r)=0.25;
end
end
end
end
end

Akzeptierte Antwort

Chunru
Chunru am 27 Aug. 2021
Bearbeitet: Chunru am 27 Aug. 2021
There is no problem on your code.
% Generate some data
A0 = mat2cell(randn(40,40), [10 10 10 10], [10 10 10 10])
A0 = 4×4 cell array
{10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double}
A = A0;
for i=1:4
for j=1:4
for k=1:10
for r=1:10
if A{i,j}(k,r)> 1
A{i,j}(k,r)=0.25;
end
end
end
end
end
A0{1,1}
ans = 10×10
-0.6734 0.8692 -1.6546 1.2093 -0.1341 -1.0695 -0.8146 2.6268 -1.6082 -0.1045 -1.2485 0.0057 -1.4931 -1.4591 -0.6572 2.0670 -1.0974 -1.5611 0.2182 -0.1246 1.8132 -0.0679 -1.3699 1.2575 0.2293 0.1918 0.6182 1.6094 -0.8047 0.5807 1.1543 -0.5832 1.1783 -0.7724 0.1431 -0.2715 0.5826 -0.0001 0.9344 0.2417 -0.0182 -0.3490 0.9430 -0.1741 -0.3922 -0.4503 -0.5467 -0.6232 -0.4263 0.1276 -0.0209 0.4221 1.1669 -0.3341 -1.6741 -0.6565 0.5037 -0.0330 -0.4805 0.0410 0.7771 0.1379 0.3202 -0.8057 -1.0736 -0.2393 -0.0838 0.6039 0.9621 0.3609 0.5417 0.7622 -1.3825 -1.2397 -0.1665 -0.7916 -0.2269 1.4506 -0.1991 -1.7380 0.8511 0.5444 -1.6257 1.8856 -0.4868 -0.7078 -0.6926 0.4313 -1.4832 1.0208 0.9372 -0.9295 1.3307 1.0626 0.9382 -1.3204 0.0972 1.0081 -0.2195 -1.8600
A{1,1} % compare A0 and A1 to see the value of 0.2500
ans = 10×10
-0.6734 0.8692 -1.6546 0.2500 -0.1341 -1.0695 -0.8146 0.2500 -1.6082 -0.1045 -1.2485 0.0057 -1.4931 -1.4591 -0.6572 0.2500 -1.0974 -1.5611 0.2182 -0.1246 0.2500 -0.0679 -1.3699 0.2500 0.2293 0.1918 0.6182 0.2500 -0.8047 0.5807 0.2500 -0.5832 0.2500 -0.7724 0.1431 -0.2715 0.5826 -0.0001 0.9344 0.2417 -0.0182 -0.3490 0.9430 -0.1741 -0.3922 -0.4503 -0.5467 -0.6232 -0.4263 0.1276 -0.0209 0.4221 0.2500 -0.3341 -1.6741 -0.6565 0.5037 -0.0330 -0.4805 0.0410 0.7771 0.1379 0.3202 -0.8057 -1.0736 -0.2393 -0.0838 0.6039 0.9621 0.3609 0.5417 0.7622 -1.3825 -1.2397 -0.1665 -0.7916 -0.2269 0.2500 -0.1991 -1.7380 0.8511 0.5444 -1.6257 0.2500 -0.4868 -0.7078 -0.6926 0.4313 -1.4832 0.2500 0.9372 -0.9295 0.2500 0.2500 0.9382 -1.3204 0.0972 0.2500 -0.2195 -1.8600
% However the code can be simplified.
A2 = cellfun(@changevalue, A0, 'UniformOutput', false);
A2{1,1}
ans = 10×10
-0.6734 0.8692 -1.6546 0.2500 -0.1341 -1.0695 -0.8146 0.2500 -1.6082 -0.1045 -1.2485 0.0057 -1.4931 -1.4591 -0.6572 0.2500 -1.0974 -1.5611 0.2182 -0.1246 0.2500 -0.0679 -1.3699 0.2500 0.2293 0.1918 0.6182 0.2500 -0.8047 0.5807 0.2500 -0.5832 0.2500 -0.7724 0.1431 -0.2715 0.5826 -0.0001 0.9344 0.2417 -0.0182 -0.3490 0.9430 -0.1741 -0.3922 -0.4503 -0.5467 -0.6232 -0.4263 0.1276 -0.0209 0.4221 0.2500 -0.3341 -1.6741 -0.6565 0.5037 -0.0330 -0.4805 0.0410 0.7771 0.1379 0.3202 -0.8057 -1.0736 -0.2393 -0.0838 0.6039 0.9621 0.3609 0.5417 0.7622 -1.3825 -1.2397 -0.1665 -0.7916 -0.2269 0.2500 -0.1991 -1.7380 0.8511 0.5444 -1.6257 0.2500 -0.4868 -0.7078 -0.6926 0.4313 -1.4832 0.2500 0.9372 -0.9295 0.2500 0.2500 0.9382 -1.3204 0.0972 0.2500 -0.2195 -1.8600
% a local function
function x = changevalue( x)
x(x>1) = 0.25;
end

Weitere Antworten (2)

KSSV
KSSV am 27 Aug. 2021
[m,n] = size(A) ;
iwant = cell(m,n);
for i = 1:m
for j = 1:n
T = A{i,j} ;
T(T>1) = 0.25 ;
iwant{i,j} = T ;
end
end

Wan Ji
Wan Ji am 27 Aug. 2021
You can do like this
A = mat2cell(rand(40,40)+0.5, [10 10 10 10], [10 10 10 10]);
% A is your data randomly generated, you can put yours there
[i,j] = meshgrid((1:size(A,1)), (1:size(A,2)));
B = arrayfun(@(i,j)A{i,j}-A{i,j}.*(A{i,j}>1) + 0.25*(A{i,j}>1), i, j, 'uniform',0); % B is what you want
Or you can do like this
A = cell2mat(A);
A(A>1) = 0.25;
A = mat2cell(A,10*ones(4,1),10*ones(4,1)); % the final is what you want

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by