How to use "if statement" for different matrix?

1 visualizzazione (ultimi 30 giorni)
sky walker
sky walker il 14 Lug 2021
Commentato: sky walker il 17 Lug 2021
i dont know how to explain my question,
lets say i have 4 matrix
a = [1 3 5; 2 4 7];
b = [1 2 5; 2 4 6];
c = [1 4 5; 4 4 7];
d = [1 3 1; 2 4 1];
i want use like
for i=1:4
if a, b and d then
x=2+n
AK= a*n*x
BK= b*n*x
DK=b*n*x
else (only c)
x=7+n
CK=c*n*x
end
end
kind of like that?
hope you understand. Thanks
  2 Commenti
Steven Lord
Steven Lord il 14 Lug 2021
What do you mean by "if a, b and d then"? If a, b, and d what?
  • If a, b, and d exist
  • If a, b, and d are the same size
  • If a, b, and d all contain the same values
  • If a, b, and d all contain nonzero values
Or do you want to perform the same steps for a, b, and d and to perform a different set of steps for c?
sky walker
sky walker il 14 Lug 2021
yes, i want to exclude c, because c have different step than other

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 14 Lug 2021
Modificato: Stephen23 il 14 Lug 2021
The MATLAB approach is to use arrays and indexing, e.g. using one simple cell array:
V = {[1,3,5;2,4,7];[1,2,5;2,4,6];[1,4,5;4,4,7];[1,3,1;2,4,1]};
W = V;
n = 1;
for k = 1:numel(V)
if k<4
x = 2+n;
else
x = 7+n;
end
W{k} = V{k}*n*x;
end
W{:}
ans = 2×3
3 9 15 6 12 21
ans = 2×3
3 6 15 6 12 18
ans = 2×3
3 12 15 12 12 21
ans = 2×3
8 24 8 16 32 8
Or by just using numeric arrays (which is probably what you should be doing, compare how much simpler and more efficient your MATLAB code would be):
A = cat(3,V{:});
U = cat(3,2,2,2,7);
B = A.*n.*(U+n)
B =
B(:,:,1) = 3 9 15 6 12 21 B(:,:,2) = 3 6 15 6 12 18 B(:,:,3) = 3 12 15 12 12 21 B(:,:,4) = 8 24 8 16 32 8
  5 Commenti
Stephen23
Stephen23 il 14 Lug 2021
Modificato: Stephen23 il 14 Lug 2021
"My code is actually simple,but just generates a lot of new matrices."
Hopefully in one cell array or structure, otherwise you really have a major design problem.
"I've seen your script, but what i need to be different is matrix c, not d."
My answer does not have matrix c nor d.
Instead of having lots of separate variables (bad data design, difficult to extend to lots of matrices) I simply placed all of your matrices into one cell array (much better data design, works automatically for any number of matrices) or into one ND-array (ditto). Both of these data designs makes it trivial and efficient to loop over your matrices (unlike your complex approach) and also to apply different steps to whichever of those matrices that you want (in fact, which ones does not even need to be hardcoded).
In my answer the fourth matrix has a different step, just as you requested.
You can make any matrix/matrices that you want have different step sizes.
As far as I can tell, my does what you need**, just simpler, more efficient, and more versatile than your approach (and also trivially extends to any number of matrices, unlike your approach).
** or atleast something very similar to it, so similar that you should not have any problems tweaking it.
sky walker
sky walker il 17 Lug 2021
ok thanks for your help stephen

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by