- Initialize the matrix 'f' with two zeros followed by values from 5 to 130 in increments of 5.
- Iterate over the matrix 'f' in reverse, starting from the last non-zero element.
- In each iteration, multiply the current element by 2 and add it to each of the preceding elements, ensuring not to include the initial zeros.
- Store the results of these computations in an output array.
- Display the final output array containing all calculated combinations.
I want to run a loop through a matrix (28x1) that inputs variable from the loop into an expression.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to run a loop through a matrix (28x1) that inputs variables from the loop into an expression.
I have a matrix that is 28x1. In the matrix I have (1,1), (2,1) as 0. (3:28,1) = 5:5:130
f=w(1:Inp+2,1) It is Inp + 2 because of the two zeros
I have a for loop of i=1:length(f)-1
I want to take the last number (end) multiply it by 2 and add the second to the last number (end-i) and give me an output. Then do the same thing but add the third to the last number(end-i). I want it to run till it hits zero which are the two top variables in the matrix.
I then want it to take the second to last number(end-1) multiply by 2 and add the third to the last number.
and follow the same pattern until 0
Example: I want to input 4
f=(1:6,1)
0
0
5
10
15
20
The first output is 55 (20*2+15 )
Second 50 (20*2+10)
45 (20*2+5)
40 (20*2+0)
Fith 40 (15*2+10)
35 (15*2 +5)
I would love if It could also do (End+ end-1+end-2) (20+15+10)
For each
The end goal is to know every combination of three variables but only one can repeat. Ex(20*2+5) cannot have (20*3).
0 Commenti
Risposte (1)
Abhas
il 15 Feb 2024
Hi Jack,
To store all the combinations of every triplets after performing the calculations, you can follow these steps in MATLAB: :
Here's the MATLAB code to reflect the above steps:
% Initialize the matrix f
f = [0; 0; (5:5:130)'];
% Initialize output array to store the results
output = [];
% Loop through the matrix f for all elements
for idx = 1:length(f)
% Start with the last element, then move to the second-to-last, and so on
last_element_index = length(f) - idx + 1;
% Multiply the selected element by 2 and add the preceding elements
for i = 1:last_element_index-1
if last_element_index - i > 1 % Ensure we don't include the two zeros
output = [output; 2*f(last_element_index) + f(last_element_index-i)];
end
end
end
% Display the output
disp('Output:');
disp(output);
This code will provide you with the output that includes all the combinations as described, ensuring that no element is used more than twice in any sum.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!