- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
using if else statements to assign a value to a variable based on the value of a specific element in a matrix
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I've created a matrix and I'm trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here's what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here's an example of how I'm trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I'm trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9
0 Commenti
Risposta accettata
Hassaan
il 19 Apr 2024
Modificato: Hassaan
il 19 Apr 2024
An initial idea:
% Example matrix M
M = randn(10,10); % A 10x10 matrix with random values
% Initialize matrix p with the same size as M
p = zeros(size(M));
% Assuming y and z1 are vectors that map to indices in M
y = 1:length(M); % Example index mapping, adjust as per your requirements
z1 = 1:length(M); % Example index mapping, adjust as per your requirements
% Loop over the indices
for j = 1:length(y)
for k = 1:length(z1)
if M(y(j), z1(k)) > 0
p(y(j), z1(k)) = 0.9;
elseif M(y(j), z1(k)) < 0
p(y(j), z1(k)) = 0.1;
else
p(y(j), z1(k)) = 0.5;
end
end
end
disp(p)
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
Feel free to contact me.
3 Commenti
Hassaan
il 19 Apr 2024
Yes. Adjust as per your needs.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!